Automatically transform arrays into objects (Field Models/Data Transfer Objects) and gain automatic type casting, type safe models and portable objects. You never have to remember an array index name again. Directly pass in matching ACF fields and that's it:
// [ 'url' => 'https://tri.be', 'title' => 'Tribe', 'target' => '_self' ]
$link = new \Tribe\Libs\Field_Models\Models\Link( get_field( 'my_link_field' ) );
// These all auto complete for you in your IDE so you no longer have to remember
// what properties are available for an ACF field that returns an array.
echo $link->url;
echo $link->title;
echo $link->target;
We've been using these in a number of projects already but I've had to manually bring them and convert everything, so this should be the last time :smile:.
T3 has a few new projects starting soon and this will save us a lot of time once it's built into the framework from the start.
Adds custom Field Models/Collections for blocks and components.
Updates WordPress to 5.9.3, ACF to 5.12.2 and Yoast to 18.8.
Hides deprecation notices when running lefthook phpcs, which appear if you're running PHP8.1 locally.
Converts all blocks/components to use Field Models, where appropriate.
General code clean up, removal of constants, variables and methods that aren't in use + escaping.
Why Field Models / DTO's?
Field models/DTO's remove the inconsistent typing from ACF and allows you populate a Field Model with a single get_field() call. You'll always get the expected type back from a Field Model, e.g. $link->url will always be a string.
Simpler and much less code (see before and after below). Fewer model/controller constants and because the Field Model travels through these instances, you only have to add a new property to the Field Model and then you can use it in the controller or view without adding additional controller and model constants/properties.
Collections let you create many of the same Field Model, with full IDE completion, assuming you type hint the collection you made. example. Collections are extremely memory efficient, as they are iterators out of the box, so they can hold an extreme amount of data, and will use minimal memory when foreach() looping over them. They can be converted to an array, which will utilize full memory when absolutely necessary. Read more about collections.
It's easy to make your own Field Models and Collections. Just extend the Field_Model class, add typed properties that match your ACF field names and your data will get auto-populated based on what get_field() returns.
These are the ACF models I've made so far out of the box for ACF, but any ACF field that returns an array can be made into one, so if you have any ideas, please submit a PR. If you want to know how things work under the hood, I think the integration tests do a good job of that.
You can bring the ACF models and DTO library into an older project if you wish, as long as it's PHP7.4+ with so composer require moderntribe/square1-field-models.
Code Examples
Some before and afters showing reduced code required.
// get_field() outputs this data: [ 'url' => 'https://tri.be', 'title' => 'Tribe', 'target' => '_self' ]
$link = new \Tribe\Libs\Field_Models\Models\Link( get_field( 'my_link_field' ) );
// Need an array, try...
$link->toArray(); // [ 'url' => 'https://tri.be', 'title' => 'Tribe', 'target' => '_self' ]
// Need only some of the field data?
$link->only( 'url' )->toArray(); // [ 'url' => 'https://tri.be' ]
$link->except( 'target' )->toArray(); // [ 'url' => 'https://tri.be', 'title' => 'Tribe' ]
QA
It's important to note field changes here are not backwards compatible (renaming ACF fields means the data no longer renders). As SquareOne is forked from its current state for new projects, this doesn't matter but would if you were testing with an existing database.
TLDR;
Automatically transform arrays into objects (Field Models/Data Transfer Objects) and gain automatic type casting, type safe models and portable objects. You never have to remember an array index name again. Directly pass in matching ACF fields and that's it:
We've been using these in a number of projects already but I've had to manually bring them and convert everything, so this should be the last time :smile:.
T3 has a few new projects starting soon and this will save us a lot of time once it's built into the framework from the start.
What does this do/fix?
Why Field Models / DTO's?
get_field()
call. You'll always get the expected type back from a Field Model, e.g. $link->url will always be a string.foreach()
looping over them. They can be converted to an array, which will utilize full memory when absolutely necessary. Read more about collections.get_field()
returns.These are the ACF models I've made so far out of the box for ACF, but any ACF field that returns an array can be made into one, so if you have any ideas, please submit a PR. If you want to know how things work under the hood, I think the integration tests do a good job of that.
Code Examples
Some before and afters showing reduced code required.
Before: CTA
After: CTA
Take an ACF Link Field as an example. If not populated,
get_field( 'my_link_field' );
can return null, false, an empty array etc...Take our Link Field Model and just pass in the field output:
QA
Tests
Does this have tests?