diff --git a/src/Services/Forms/Columns.php b/src/Services/Forms/Columns.php index 93c45d71b..9ddecaa4f 100644 --- a/src/Services/Forms/Columns.php +++ b/src/Services/Forms/Columns.php @@ -4,6 +4,7 @@ use A17\Twill\Services\Forms\Contracts\CanHaveSubfields; use A17\Twill\Services\Forms\Contracts\CanRenderForBlocks; +use A17\Twill\Services\Forms\Traits\HasSubFields; use A17\Twill\Services\Forms\Traits\RenderForBlocks; use Illuminate\Contracts\View\View; use Illuminate\Support\Collection; @@ -12,6 +13,7 @@ class Columns implements CanHaveSubfields, CanRenderForBlocks { use RenderForBlocks; + use HasSubFields; protected function __construct( public ?Collection $left = null, @@ -66,37 +68,8 @@ public function render(): View public function registerDynamicRepeaters(): void { - if ($this->left) { - foreach ($this->left as $field) { - if ($field instanceof InlineRepeater) { - $field->register(); - } - if ($field instanceof CanHaveSubfields) { - $field->registerDynamicRepeaters(); - } - } - } - - if ($this->middle) { - foreach ($this->middle as $field) { - if ($field instanceof InlineRepeater) { - $field->register(); - } - if ($field instanceof CanHaveSubfields) { - $field->registerDynamicRepeaters(); - } - } - } - - if ($this->right) { - foreach ($this->right as $field) { - if ($field instanceof InlineRepeater) { - $field->register(); - } - if ($field instanceof CanHaveSubfields) { - $field->registerDynamicRepeaters(); - } - } - } + $this->registerDynamicRepeatersFor($this->left); + $this->registerDynamicRepeatersFor($this->middle); + $this->registerDynamicRepeatersFor($this->right); } } diff --git a/src/Services/Forms/Fieldset.php b/src/Services/Forms/Fieldset.php index 21de350d5..986b1e30d 100644 --- a/src/Services/Forms/Fieldset.php +++ b/src/Services/Forms/Fieldset.php @@ -3,11 +3,14 @@ namespace A17\Twill\Services\Forms; use A17\Twill\Services\Forms\Contracts\CanHaveSubfields; +use A17\Twill\Services\Forms\Traits\HasSubFields; use Illuminate\Support\Collection; use Illuminate\Support\Str; class Fieldset implements CanHaveSubfields { + use HasSubFields; + protected function __construct( public ?string $title = null, public ?Collection $fields = null, @@ -77,13 +80,6 @@ public function fields(array $fields): static public function registerDynamicRepeaters(): void { - foreach ($this as $field) { - if ($field instanceof InlineRepeater) { - $field->register(); - } - if ($field instanceof CanHaveSubfields) { - $field->registerDynamicRepeaters(); - } - } + $this->registerDynamicRepeatersFor($this->fields); } } diff --git a/src/Services/Forms/Fieldsets.php b/src/Services/Forms/Fieldsets.php index 2ca553f66..82099c4e9 100644 --- a/src/Services/Forms/Fieldsets.php +++ b/src/Services/Forms/Fieldsets.php @@ -2,8 +2,11 @@ namespace A17\Twill\Services\Forms; +use A17\Twill\Services\Forms\Contracts\CanHaveSubfields; +use A17\Twill\Services\Forms\Traits\HasSubFields; use Illuminate\Support\Collection; -class Fieldsets extends Collection +class Fieldsets extends Collection implements CanHaveSubfields { + use HasSubFields; } diff --git a/src/Services/Forms/Form.php b/src/Services/Forms/Form.php index f8498cd81..8c02e213b 100644 --- a/src/Services/Forms/Form.php +++ b/src/Services/Forms/Form.php @@ -3,11 +3,14 @@ namespace A17\Twill\Services\Forms; use A17\Twill\Services\Forms\Contracts\CanHaveSubfields; +use A17\Twill\Services\Forms\Traits\HasSubFields; use Illuminate\Contracts\View\View; use Illuminate\Support\Collection; class Form extends Collection implements CanHaveSubfields { + use HasSubFields; + public ?Fieldsets $fieldsets = null; private ?Form $sideForm = null; @@ -111,27 +114,7 @@ public function renderSideForm(): View public function registerDynamicRepeaters(): void { - // We have to loop over all of the fields/fieldsets. - foreach ($this as $field) { - if ($field instanceof InlineRepeater) { - $field->register(); - } - if ($field instanceof CanHaveSubfields) { - $field->registerDynamicRepeaters(); - } - } - - if ($this->fieldsets) { - foreach ($this->fieldsets as $fieldset) { - foreach ($fieldset->fields as $field) { - if ($field instanceof InlineRepeater) { - $field->register(); - } - if ($field instanceof CanHaveSubfields) { - $field->registerDynamicRepeaters(); - } - } - } - } + $this->registerDynamicRepeatersFor($this); + $this->fieldsets?->registerDynamicRepeaters(); } } diff --git a/src/Services/Forms/InlineRepeater.php b/src/Services/Forms/InlineRepeater.php index e0bad64cb..13358df89 100644 --- a/src/Services/Forms/InlineRepeater.php +++ b/src/Services/Forms/InlineRepeater.php @@ -7,6 +7,7 @@ use A17\Twill\Services\Forms\Contracts\CanHaveSubfields; use A17\Twill\Services\Forms\Contracts\CanRenderForBlocks; use A17\Twill\Services\Forms\Fields\Repeater; +use A17\Twill\Services\Forms\Traits\HasSubFields; use A17\Twill\Services\Forms\Traits\RenderForBlocks; use Illuminate\Contracts\View\View; use Illuminate\Support\Collection; @@ -15,6 +16,7 @@ class InlineRepeater implements CanHaveSubfields, CanRenderForBlocks { use RenderForBlocks; + use HasSubFields; protected function __construct( private ?string $name = null, @@ -225,14 +227,8 @@ public function render(): View public function registerDynamicRepeaters(): void { - foreach ($this->fields as $field) { - if ($field instanceof self) { - $field->register(); - } - if ($field instanceof CanHaveSubfields) { - $field->registerDynamicRepeaters(); - } - } + $this->register(); + $this->registerDynamicRepeatersFor($this->fields); } diff --git a/src/Services/Forms/Traits/HasSubFields.php b/src/Services/Forms/Traits/HasSubFields.php new file mode 100644 index 000000000..7efd17776 --- /dev/null +++ b/src/Services/Forms/Traits/HasSubFields.php @@ -0,0 +1,27 @@ +registerDynamicRepeatersFor($this); + } + } + + protected function registerDynamicRepeatersFor(?iterable $fields): void + { + if ($fields) { + foreach ($fields as $field) { + if ($field instanceof CanHaveSubfields) { + $field->registerDynamicRepeaters(); + } + } + } + } +} diff --git a/tests/unit/Services/Forms/NestingTest.php b/tests/unit/Services/Forms/NestingTest.php new file mode 100644 index 000000000..ae33ed12a --- /dev/null +++ b/tests/unit/Services/Forms/NestingTest.php @@ -0,0 +1,40 @@ +middle([ + InlineRepeater::make()->name('repeater1') + ]), + InlineRepeater::make()->name('repeater2'), + ])->addFieldset( + Fieldset::make()->fields([ + Columns::make()->left([ + InlineRepeater::make()->name('set-repeater1')->fields([ + InlineRepeater::make()->name('nested1') + ]) + ])->right([ + InlineRepeater::make()->name('set-repeater2') + ]) + ]) + ); + $form->registerDynamicRepeaters(); + $this->assertArrayHasKey('repeater1', TwillBlocks::$dynamicRepeaters); + $this->assertArrayHasKey('repeater2', TwillBlocks::$dynamicRepeaters); + $this->assertArrayHasKey('set-repeater1', TwillBlocks::$dynamicRepeaters); + $this->assertArrayHasKey('nested1', TwillBlocks::$dynamicRepeaters); + $this->assertArrayHasKey('set-repeater2', TwillBlocks::$dynamicRepeaters); + } +}