diff --git a/README.md b/README.md index 98c5f245..d20eb4d3 100644 --- a/README.md +++ b/README.md @@ -47,12 +47,12 @@ $ wp acorn vendor:publish --tag="acf-composer" To create your first field group, start by running the following generator command from your theme directory: ```bash -$ wp acorn acf:field Example +$ wp acorn acf:field ExampleField ``` -This will create `src/Fields/Example.php` which is where you will create and manage your first field group. +This will create `src/Fields/ExampleField.php` which is where you will create and manage your first field group. -Taking a glance at the generated `Example.php` stub, you will notice that it has a simple list configured. +Taking a glance at the generated `ExampleField.php` stub, you will notice that it has a simple list configured. ```php setLocation('post_type', '==', 'post'); - $example + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $example->build(); + return $fields->build(); } } ``` @@ -115,14 +115,14 @@ class ListItems extends Partial */ public function fields() { - $listItems = Builder::make('listItems'); + $fields = Builder::make('listItems'); - $listItems + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $listItems; + return $fields; } } ``` @@ -151,15 +151,15 @@ class Example extends Field */ public function fields() { - $example = Builder::make('example'); + $fields = Builder::make('example'); - $example + $fields ->setLocation('post_type', '==', 'post'); - $example + $fields ->addPartial(ListItems::class); - return $example->build(); + return $fields->build(); } } ``` @@ -171,7 +171,7 @@ Generating a block is generally the same as generating a field as seen above. Start by creating the block field using Acorn: ```bash -$ wp acorn acf:block Example +$ wp acorn acf:block ExampleBlock ``` ```php @@ -182,14 +182,14 @@ namespace App\Blocks; use Log1x\AcfComposer\Block; use Log1x\AcfComposer\Builder; -class Example extends Block +class ExampleBlock extends Block { /** * The block name. * * @var string */ - public $name = 'Example'; + public $name = 'Example Block'; /** * The block description. @@ -231,14 +231,14 @@ class Example extends Block */ public function fields() { - $example = Builder::make('example'); + $fields = Builder::make('example_block'); - $example + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $example->build(); + return $fields->build(); } /** @@ -297,7 +297,7 @@ Creating a sidebar widget using ACF Composer is extremely easy. Widgets are auto Start by creating a widget using Acorn: ```bash -$ wp acorn acf:widget Example +$ wp acorn acf:widget ExampleWidget ``` ```php @@ -308,14 +308,14 @@ namespace App\Widgets; use Log1x\AcfComposer\Builder; use Log1x\AcfComposer\Widget; -class Example extends Widget +class ExampleWidget extends Widget { /** * The widget name. * * @var string */ - public $name = 'Example'; + public $name = 'Example Widget'; /** * The widget description. @@ -352,17 +352,17 @@ class Example extends Widget */ public function fields() { - $example = Builder::make('example'); + $fields = Builder::make('example_widget'); - $example + $fields ->addText('title'); - $example + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $example->build(); + return $fields->build(); } /** @@ -400,7 +400,7 @@ Creating an options page is similar to creating a regular field group in additio Start by creating an option page using Acorn: ```bash -$ wp acorn acf:options Example +$ wp acorn acf:options ExampleOptions ``` ```php @@ -411,21 +411,21 @@ namespace App\Options; use Log1x\AcfComposer\Builder; use Log1x\AcfComposer\Options as Field; -class Example extends Field +class ExampleOptions extends Field { /** * The option page menu name. * * @var string */ - public $name = 'Example'; + public $name = 'Example Options'; /** * The option page document title. * * @var string */ - public $title = 'Example | Options'; + public $title = 'Example Options | Options'; /** * The option page field group. @@ -434,14 +434,14 @@ class Example extends Field */ public function fields() { - $example = Builder::make('example'); + $fields = Builder::make('example_options'); - $example + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $example->build(); + return $fields->build(); } } ``` @@ -491,7 +491,7 @@ The publish command generates all available stubs by default. However, each stub ## Default Field Settings -One of my personal favorite features of ACF Composer is the ability to set field type as well as field group defaults. Any globally set default can of course be over-ridden by simply setting it on the individual field. +A useful feature unique to ACF Composer is the ability to set field type as well as field group defaults. Any globally set default can of course be over-ridden by simply setting it on the individual field. ### Global diff --git a/src/AcfComposer.php b/src/AcfComposer.php index 237f92b9..9b1a212e 100644 --- a/src/AcfComposer.php +++ b/src/AcfComposer.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\File; use Illuminate\Support\Str; +use Log1x\AcfComposer\Exceptions\DuplicateKeyException; use ReflectionClass; use Roots\Acorn\Application; use Symfony\Component\Finder\Finder; @@ -150,6 +151,30 @@ public function handleComposers(): void $this->deferredOptions = []; $this->pendingComposers = []; + + foreach ($this->composers as $namespace => $composers) { + $names = []; + + foreach ($composers as $composer) { + $group = $composer->getFields(); + + $key = $group['key'] ?? $group[0]['key'] ?? null; + + if (! $key) { + continue; + } + + if (isset($names[$key])) { + $class = $composer::class; + + throw new DuplicateKeyException("Duplicate ACF field group key [{$key}] found in [{$class}] and [{$names[$key]}]."); + } + + $names[$key] = $composer::class; + } + + $this->composers[$namespace] = array_values($composers); + } } /** diff --git a/src/Composer.php b/src/Composer.php index a7135df7..c1ae495f 100644 --- a/src/Composer.php +++ b/src/Composer.php @@ -2,12 +2,12 @@ namespace Log1x\AcfComposer; -use Exception; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Log1x\AcfComposer\Concerns\InteractsWithPartial; use Log1x\AcfComposer\Contracts\Composer as ComposerContract; use Log1x\AcfComposer\Contracts\Field as FieldContract; +use Log1x\AcfComposer\Exceptions\InvalidFieldsException; use Roots\Acorn\Application; use StoutLogic\AcfBuilder\FieldsBuilder; @@ -129,7 +129,7 @@ public function getFields(bool $cache = true): array : $fields; if (! is_array($fields)) { - throw new Exception('Fields must be an array or an instance of Builder.'); + throw new InvalidFieldsException; } if ($this->defaults->has('field_group')) { diff --git a/src/Console/stubs/block.construct.stub b/src/Console/stubs/block.construct.stub index 0f77b761..1a22fd62 100644 --- a/src/Console/stubs/block.construct.stub +++ b/src/Console/stubs/block.construct.stub @@ -78,14 +78,14 @@ class DummyClass extends Block */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } /** diff --git a/src/Console/stubs/block.stub b/src/Console/stubs/block.stub index 38d96c4f..4c4002c9 100644 --- a/src/Console/stubs/block.stub +++ b/src/Console/stubs/block.stub @@ -157,14 +157,14 @@ class DummyClass extends Block */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } /** diff --git a/src/Console/stubs/field.stub b/src/Console/stubs/field.stub index ccea053b..6c793ecf 100644 --- a/src/Console/stubs/field.stub +++ b/src/Console/stubs/field.stub @@ -12,16 +12,16 @@ class DummyClass extends Field */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->setLocation('post_type', '==', 'post'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } } diff --git a/src/Console/stubs/options.full.stub b/src/Console/stubs/options.full.stub index fe5bc96e..9c9b860d 100644 --- a/src/Console/stubs/options.full.stub +++ b/src/Console/stubs/options.full.stub @@ -112,13 +112,13 @@ class DummyClass extends Field */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } } diff --git a/src/Console/stubs/options.stub b/src/Console/stubs/options.stub index da5d0141..e80f53cb 100644 --- a/src/Console/stubs/options.stub +++ b/src/Console/stubs/options.stub @@ -26,13 +26,13 @@ class DummyClass extends Field */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } } diff --git a/src/Console/stubs/partial.stub b/src/Console/stubs/partial.stub index 70e4ab81..8b999e30 100644 --- a/src/Console/stubs/partial.stub +++ b/src/Console/stubs/partial.stub @@ -12,13 +12,13 @@ class DummyClass extends Partial */ public function fields(): Builder { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel; + return $fields; } } diff --git a/src/Console/stubs/widget.stub b/src/Console/stubs/widget.stub index 261304ad..947c83f4 100644 --- a/src/Console/stubs/widget.stub +++ b/src/Console/stubs/widget.stub @@ -44,17 +44,17 @@ class DummyClass extends Widget */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addText('title'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } /** diff --git a/src/Exceptions/DuplicateKeyException.php b/src/Exceptions/DuplicateKeyException.php new file mode 100644 index 00000000..7e6585a2 --- /dev/null +++ b/src/Exceptions/DuplicateKeyException.php @@ -0,0 +1,10 @@ +