moderntribe/square1-field-models

Data Transfer Objects (models) and Collections for ACF

4.2.0 2022-10-25 16:44 UTC

This package is auto-updated.

Last update: 2024-03-25 19:42:47 UTC


README

Utilizes Spatie Data Transfer Objects v2 to provide models for ACF fields that support arrays, avoiding needing to remember these field structures and providing autocompletion for your IDE.

Usage

Simply create a class that extends Field_Model, set your properties and types to match what ACF returns when it fetches the array of field data, pass that array to your model and it will get automatically populated, allowing you to use the class properties to access the data.

Examples

Imagine you have an ACF User Field. Simply pass the ACF field data to the existing User Model, for example:

$user = new \Tribe\Libs\Field_Models\Models\User( (array) get_field( 'user_field' ) );

echo $user->ID; // e.g. 22
echo $user->user_firstname; // e.g. Steve
// etc...

You can also make Collections, which will take ACF Repeater data. Expanding on the User example above, imagine you have a repeater of User fields, simply pass the repeater data directly to the collection's create method:

$user_collection = \Tribe\Libs\Field_Models\Collections\User_Collection::create( (array) get_field( 'user_repeater' ) );

foreach ( $user_collection as $user ) {
    echo $user->ID; 
}

Requirements

  • PHP 7.4+
  • Advanced Custom Fields Or, the PRO version for repeaters