Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Throw an exception if field group keys are not unique (Fixes #273) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Aug 12, 2024
2 parents 3691d55 + 00e3d68 commit 84faf1f
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 63 deletions.
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
Expand All @@ -62,7 +62,7 @@ namespace App\Fields;
use Log1x\AcfComposer\Builder;
use Log1x\AcfComposer\Field;

class Example extends Field
class ExampleField extends Field
{
/**
* The field group.
Expand All @@ -71,17 +71,17 @@ class Example extends Field
*/
public function fields()
{
$example = Builder::make('example');
$fields = Builder::make('example_field');

$example
$fields
->setLocation('post_type', '==', 'post');

$example
$fields
->addRepeater('items')
->addText('item')
->endRepeater();

return $example->build();
return $fields->build();
}
}
```
Expand Down Expand Up @@ -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;
}
}
```
Expand Down Expand Up @@ -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();
}
}
```
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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();
}
}
```
Expand Down Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions src/AcfComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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')) {
Expand Down
6 changes: 3 additions & 3 deletions src/Console/stubs/block.construct.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Console/stubs/block.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Console/stubs/field.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
6 changes: 3 additions & 3 deletions src/Console/stubs/options.full.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
6 changes: 3 additions & 3 deletions src/Console/stubs/options.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Loading

0 comments on commit 84faf1f

Please sign in to comment.