Fields
Forme is tightly integrated with Advanced Custom Fields, and includes a few conventions and some useful magic for handling fields.
Creating a field group
Field groups live in app/Fields
and should implement Forme/Framework/Fields/FieldGroupInterface
. They have a single method, add
, into which you can copy the php from the export feature in ACF.
// app/Fields/FooBarFielGroup.php
declare(strict_types=1);
namespace YourNameSpace\YourApp\Fields;
use Forme\Framework\Fields\FieldGroupInterface;
final class FooBarFieldGroup implements FieldGroupInterface
{
public function add(): void
{
acf_add_local_field_group([
'key' => 'group_5fc6297c4b8cb',
'title' => 'Foo',
'fields' => ['etc'],
]);
}
}
TIP
Don't put more than one field group per class. Also try to keep field groups as small as possible when you're configuring your forms in acf itself. You want to keep things composable and maintainable.
Field Registry
The field registry class takes care of adding all your field groups, it globs the classes in app\Fields
on the acf/init
hook, and cycles through each one, calling add
.
Automatic injection into Template Controllers
ACF Fields for the queried object are automatically injected into Template Controllers - take a look at $request['fields']
.
Otherwise, call get_field
or get_fields
as you would do normally.
TIP
You shouldn't ever need to use ACF's get_sub_field
function, especially since we really recommend not using its have_rows
antipattern in the first place. Try to refactor if you start reaching for either of those.
Code Generation
You can create a new field group via the cli:
forme make field FooBar
This will make a new field group class YourNameSpace\YourApp\Fields\FooBarFieldGroup
ready to populate.