stoutlogic/acf-builder
Fluent builder for Advanced Custom Fields Pro (ACF) field groups. Define fields and locations in PHP, reuse configs, and generate ACF’s registration arrays with less boilerplate. Ideal for keeping ACF setup in version control.
A frequent requested feature was the ability to modify and remove nested fields. This is now possible with adding the getField method to the GroupBuilder and getLayout to the FlexibleContentBuilder. Also introduced is a short hand so you don't have to chain getField calls. Using the -> delimiter you can directly reach the field you're right to modify or remove.
Example:
$builder->removeField('sections->hero->sub_title');
There was discussion of using a . dot delimiter instead for the short hand, but I decided to use the arrow in case people had used a . in their existing field names. I didn't want them to experience unintended consequences. When releasing a major version with breaking changes we will revisit this possibility.
Check out the docs for it here: https://github.com/StoutLogic/acf-builder/wiki/modifying-fields#modifying-nested-fields
Thanks @Androlax2 @voyou-sbeaulieu @marcelo2605 for your contributions and patience
Merged in some PRs: #133 and #137
Thanks @titouanmathis and @davddo
You can now write conditionals that reference fields not on the same level. [Issue: #52]
You can either just reference the name of the parent field:
$builder = new \StoutLogic\AcfBuilder\FlexibleContentBuilder('sections');
$builder
->addLayout('hero')
->addSelect('hero_type')
->addChoices('fullscreen', 'standard')
->addImage( 'hero_image' )
->addWysiwyg( 'hero_text' )
->addRepeater('cta')
->addSelect('link_type')
->addChoices('internal', 'external', 'text')
->addTrueFalse('cta_animated')
->conditional('hero_type', '==', 'fullscreen');
Or you can set a custom key
$builder = new \StoutLogic\AcfBuilder\FlexibleContentBuilder('sections');
$builder
->addLayout('hero')
->addSelect('hero_type')
->addChoices('fullscreen', 'standard')
->setCustomKey('my_custom_key')
->addImage( 'hero_image' )
->addWysiwyg( 'hero_text' )
->addRepeater('cta')
->addSelect('link_type')
->addChoices('internal', 'external', 'text')
->addTrueFalse('cta_animated')
->conditional('my_custom_key', '==', 'fullscreen');
This shouldn't break backwards compatibility, I've tested it on my professional projects. But if you do run into issues, let me know ASAP.
Other additions include:
Add support for doctrine/inflector ^2.0 and some code cleanup, while maintaining backwards compatibility with Add support for doctrine/inflector ^1.1 and older versions of PHP
Thanks @Log1x !
Allows you to use ->setLabel('My Custom Label') on a FieldBuilder. Thanks @Log1x and sorry for the delay.
ACF Builder relies on "doctrine/inflector": "^1.1", "doctrine/instantiator": "^1.0"
And now will allow newer 1.x versions to be installed along site ACF Builder. Thanks @guix77 !!
@aprokopenko has graciously added function calls to make setting field width, class, id and attributes much easier.
$builder = new FieldsBuilder('Slider')
$builder
->addText( 'slider_title' )
->setWidth( '50%' )
->setSelector( '#my-id.my-class' )
->setAttr( 'data-custom_attr', 'thisisattr' );
No more need to manually set the wrapper array as attributes to a new field.
You can use $fieldsBuilder->addButtonGroup like you would select or radio buttons. See ACF documentation for more information about Button Groups
Thanks @Log1x for the help!
Fixed issues #58 and #59
Added the new ACF Accordion field. Must be using ACF 5.6.6 to use it.
It works just like Tab field but includes two additional methods setOpen() and setMultiExpand(). true or false can be passed as a parameter, true is default.
$fieldgroup = new FieldsBuilder('group');
$fieldgroup
->addAccordion('content')
->setOpen()
->addWysiwyg('content')
->addAccordion('background')
->addImage('background_image')
->addColorPicker('background_color');
addRange methodmodifyFieldmodifyField documentation to explicitly say the passed closure needs to return a FieldsBuilderAdd new ACF fields addLink and addGroup. A group is treated just like a repeater, but instead of an endRepeater function, there is an endGroup function.
I'm releasing this before a refactor, which the code needs, because I wanted to get the functionality out there for everyone. I'm not happy with all of the inheritance, and I'm not longer happy with the confusing names FieldsBuilder vs FieldBuilder vs GroupBuilder Also I would like to use less magic methods if possible. Perhaps in a 2.0 release.
Lots of internal changes, uses an array FieldBuilder objects to story field configs instead of storing field group's field config in a simple array.
getRootContext to get the top level parentContextfieldsBuilder in a RepeaterBuilder protected so sub classes can access itgetGroupConfig function to FieldsBuilderfieldExists method to FieldsBuilderaddPostLink to addPageLink on FieldsBuilderMoved tests to autoload-dev so they aren't included in production. Issue #18 Updated composer dependencies.
Modifying and Removing Fields Added is the ability to modify, insert and remove fields from a FieldsBuilder. See the Modifying Fields page in the wiki for more information. Issue #12
Added addTimePicker and addDateTimePicker function ACF 5.3.9 added these two new fields Issue #13
Fixes #8 #11
Resolves issues #2 and #6
Fixes the setLocation bug. When setLocation is called on a builder, it will call it on the parent builder context if one exists.
To use the library without composer, simply require the included autoload.php file.
'and' and 'or' are reserved keywords in PHP. Therefore PHP gets angry when you try to use them as method names. In testing it worked fine but in the real world they did not. They are now allowed via __call, and are aliases for andCondition and orCondition methods.
Fix default keyword issue
How can I help you explore Laravel packages today?