Wallogit.com
2017 © Pedro Peláez
Advanced Custom Fields helper for WordPress
The acf component uses three classes - AbstractFieldGroup - You extend this to add acf field groups - AbstractFieldProxy - You extend this to add acf fields, it works as a proxy as the way acf fields work makes them create the hooks too early, so by using a proxy we can make it add hooks to register them at the correct time. - Helper - Helper to add the field groups and fields, fields register themselves and field groups use register_field_group to register., (*1)
Lets create a pimple config for both of all of these:, (*2)
$pimple["documentDetailsFieldGroup"] = function () {
return new DocumentDetails();
};
$pimple["flexibleLayoutProxy"] = function ($c) {
return new FlexibleLayoutProxy($c);
};
$pimple['ACFHelper'] = function ($c) {
return new ACFHelper($c['objectStorage'], $c['objectStorage'],
array(
$c['documentDetailsFieldGroup']
),
array(
$c['flexibleLayoutProxy']
));
};
Create a concreate class that implements the AbstractFieldGroup class and implements the getconfig() method., (*3)
class DocumentDetails extends AbstractFieldGroup {
public function getConfig()
{
return array(
// config would be set in here
)
}
}
What we have in the example above a class that has the method getConfig(), this returns the config array that you get when exporting a acf field group., (*4)
And that's it, next lets make a AbstractFieldProxy, (*5)
For a AbstractFieldProxy class we just need to extends the AbstractFieldProxy class and implement the getDICName method, lets do that now:, (*6)
class DocumentDetails extends AbstractFieldProxy {
public function getDICName()
{
return 'flexibleLayoutField';
}
}
What this does by extends the AbstractFieldProxy it adds a hook for acf/register_fields and uses the DIC you pass as a dependency to it along with the name you specify in the getDICName method to create an instance of the field in the acf/register_fields action., (*7)
As previously stated the AbstractFieldProxy automatically hooks itself up but for the field groups you need to call registerFieldGroups on the helper like this:, (*8)
$helper = $pimple['ACFHelper']; $helper->registerFieldGroups();