From 2ba30f2eae351e2011d9f2521d950ab9fdbac5ed Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 15 May 2024 16:34:06 +0100 Subject: [PATCH 1/9] Ensure default config values are available in form tag --- src/Fields/Fieldtype.php | 9 +++++++++ src/Tags/Concerns/RendersForms.php | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Fields/Fieldtype.php b/src/Fields/Fieldtype.php index 9c1bca046a..12ff2f0e6a 100644 --- a/src/Fields/Fieldtype.php +++ b/src/Fields/Fieldtype.php @@ -253,6 +253,15 @@ public function configFields(): Fields return new ConfigFields($fields); } + public function defaultConfigValues(): array + { + return Field::commonFieldOptions()->all() + ->merge($this->configFields()->all()) + ->filter(fn (ConfigField $configField) => $configField->get('default')) + ->map(fn (ConfigField $configField) => $configField->get('default')) + ->all(); + } + protected function configFieldItems(): array { return $this->configFields; diff --git a/src/Tags/Concerns/RendersForms.php b/src/Tags/Concerns/RendersForms.php index f80ba5c479..1ffcf28c04 100644 --- a/src/Tags/Concerns/RendersForms.php +++ b/src/Tags/Concerns/RendersForms.php @@ -135,7 +135,7 @@ protected function getRenderableField($field, $errorBag = 'default', $manipulate $default = $field->value() ?? $field->defaultValue(); $value = $old === $missing ? $default : $old; - $data = array_merge($field->toArray(), [ + $data = array_merge($field->fieldtype()->defaultConfigValues(), $field->toArray(), [ 'instructions' => $field->instructions(), 'error' => $errors->first($field->handle()) ?: null, 'default' => $field->value() ?? $field->defaultValue(), From 5edb07503fd89de35e1abe54e242bfc485470f76 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 15 May 2024 17:06:14 +0100 Subject: [PATCH 2/9] Include defaults in `Field@toArray` --- src/Fields/Field.php | 8 +++++++- src/Fields/Fieldtype.php | 9 --------- src/Tags/Concerns/RendersForms.php | 2 +- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index a2f9445006..6889c50849 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -365,7 +365,13 @@ public function shallowAugment() public function toArray() { - return array_merge($this->config, [ + $defaultValues = Field::commonFieldOptions()->all() + ->merge($this->fieldtype()->configFields()->all()) + ->filter(fn (ConfigField $configField) => $configField->get('default')) + ->map(fn (ConfigField $configField) => $configField->get('default')) + ->all(); + + return array_merge($defaultValues, $this->config, [ 'handle' => $this->handle, 'width' => $this->config['width'] ?? 100, ]); diff --git a/src/Fields/Fieldtype.php b/src/Fields/Fieldtype.php index 12ff2f0e6a..9c1bca046a 100644 --- a/src/Fields/Fieldtype.php +++ b/src/Fields/Fieldtype.php @@ -253,15 +253,6 @@ public function configFields(): Fields return new ConfigFields($fields); } - public function defaultConfigValues(): array - { - return Field::commonFieldOptions()->all() - ->merge($this->configFields()->all()) - ->filter(fn (ConfigField $configField) => $configField->get('default')) - ->map(fn (ConfigField $configField) => $configField->get('default')) - ->all(); - } - protected function configFieldItems(): array { return $this->configFields; diff --git a/src/Tags/Concerns/RendersForms.php b/src/Tags/Concerns/RendersForms.php index 1ffcf28c04..f80ba5c479 100644 --- a/src/Tags/Concerns/RendersForms.php +++ b/src/Tags/Concerns/RendersForms.php @@ -135,7 +135,7 @@ protected function getRenderableField($field, $errorBag = 'default', $manipulate $default = $field->value() ?? $field->defaultValue(); $value = $old === $missing ? $default : $old; - $data = array_merge($field->fieldtype()->defaultConfigValues(), $field->toArray(), [ + $data = array_merge($field->toArray(), [ 'instructions' => $field->instructions(), 'error' => $errors->first($field->handle()) ?: null, 'default' => $field->value() ?? $field->defaultValue(), From 1a001e3384142a922fd65bd771f8eac36c371b01 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 15 May 2024 12:50:21 -0400 Subject: [PATCH 3/9] improve test --- tests/Fields/FieldTest.php | 54 +++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/tests/Fields/FieldTest.php b/tests/Fields/FieldTest.php index edb0686ac9..dff1841765 100644 --- a/tests/Fields/FieldTest.php +++ b/tests/Fields/FieldTest.php @@ -462,14 +462,62 @@ public function defaultValue() } /** @test */ - public function converting_to_an_array_will_inline_the_handle() + public function it_converts_to_an_array() { - $field = new Field('the_handle', ['foo' => 'bar']); + (new class extends Fieldtype + { + protected static $handle = 'test'; + protected $configFields = [ + 'baz' => [ + 'type' => 'text', + 'default' => 'qux', + ], + 'alfa' => [ + 'type' => 'text', + 'default' => 'bravo', + ], + 'toggle' => [ + 'type' => 'toggle', + ], + 'grid' => [ + 'type' => 'grid', + ], + ]; + })::register(); + + $field = new Field('the_handle', [ + 'type' => 'test', + 'foo' => 'bar', + 'replicator_preview' => false, + 'alfa' => 'charlie', + ]); $this->assertEquals([ + // Handle and type are inlined. 'handle' => 'the_handle', - 'foo' => 'bar', + 'type' => 'test', + + // Width will always get merged in. 'width' => 100, + + // Explicitly set options. + 'foo' => 'bar', // Abitrary. + 'replicator_preview' => false, // A common field option. + 'alfa' => 'charlie', // A fieldtype option. + + // Defaults from common field options. + 'display' => null, + 'hide_display' => false, + 'instructions' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'visibility' => 'visible', + 'duplicate' => true, + + // Defaults from the fieldtype's config fields. + 'baz' => 'qux', + 'toggle' => false, + 'grid' => [], ], $field->toArray()); } From dc6648272026a9476f3949bde3b17249f952b119 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 15 May 2024 12:50:32 -0400 Subject: [PATCH 4/9] ensure type is inlined --- src/Fields/Field.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 6889c50849..ed4caef100 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -373,6 +373,7 @@ public function toArray() return array_merge($defaultValues, $this->config, [ 'handle' => $this->handle, + 'type' => $this->type(), 'width' => $this->config['width'] ?? 100, ]); } From ba6e5e4d5ce9da55cfc3a04ed11e1ec24d32576c Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 15 May 2024 12:51:08 -0400 Subject: [PATCH 5/9] don't filter anything out --- src/Fields/Field.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index ed4caef100..31b83bd8a4 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -367,7 +367,6 @@ public function toArray() { $defaultValues = Field::commonFieldOptions()->all() ->merge($this->fieldtype()->configFields()->all()) - ->filter(fn (ConfigField $configField) => $configField->get('default')) ->map(fn (ConfigField $configField) => $configField->get('default')) ->all(); From 30b58a30ecd15c3bf5c5d35748cf61f1bfbde383 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 15 May 2024 12:51:28 -0400 Subject: [PATCH 6/9] use the dedicated method to ensure fieldtype defaults get inherited --- src/Fields/Field.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 31b83bd8a4..78c65f9505 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -367,7 +367,7 @@ public function toArray() { $defaultValues = Field::commonFieldOptions()->all() ->merge($this->fieldtype()->configFields()->all()) - ->map(fn (ConfigField $configField) => $configField->get('default')) + ->map(fn (ConfigField $configField) => $configField->defaultValue()) ->all(); return array_merge($defaultValues, $this->config, [ From f33f73ed0299eb1cf038095345562ad1af74e045 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 15 May 2024 12:52:35 -0400 Subject: [PATCH 7/9] higher order --- src/Fields/Field.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 78c65f9505..d12f1c3a4e 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -367,7 +367,7 @@ public function toArray() { $defaultValues = Field::commonFieldOptions()->all() ->merge($this->fieldtype()->configFields()->all()) - ->map(fn (ConfigField $configField) => $configField->defaultValue()) + ->map->defaultValue() ->all(); return array_merge($defaultValues, $this->config, [ From c781f5635581648f81e947a24355017caec594ef Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 15 May 2024 12:52:38 -0400 Subject: [PATCH 8/9] nitpick --- src/Fields/Field.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index d12f1c3a4e..486e5afe5c 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -365,12 +365,12 @@ public function shallowAugment() public function toArray() { - $defaultValues = Field::commonFieldOptions()->all() + $defaults = Field::commonFieldOptions()->all() ->merge($this->fieldtype()->configFields()->all()) ->map->defaultValue() ->all(); - return array_merge($defaultValues, $this->config, [ + return array_merge($defaults, $this->config, [ 'handle' => $this->handle, 'type' => $this->type(), 'width' => $this->config['width'] ?? 100, From bb05d7353bcb85457876ec82b69cc6a6b45c06e6 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 15 May 2024 14:03:29 -0400 Subject: [PATCH 9/9] revert back to essentially what duncan had. there were a few knock on effects of changing toArray --- src/Fields/Field.php | 8 +---- src/Tags/Concerns/RendersForms.php | 8 ++++- tests/Fields/FieldTest.php | 54 ++---------------------------- 3 files changed, 11 insertions(+), 59 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 486e5afe5c..a2f9445006 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -365,14 +365,8 @@ public function shallowAugment() public function toArray() { - $defaults = Field::commonFieldOptions()->all() - ->merge($this->fieldtype()->configFields()->all()) - ->map->defaultValue() - ->all(); - - return array_merge($defaults, $this->config, [ + return array_merge($this->config, [ 'handle' => $this->handle, - 'type' => $this->type(), 'width' => $this->config['width'] ?? 100, ]); } diff --git a/src/Tags/Concerns/RendersForms.php b/src/Tags/Concerns/RendersForms.php index f80ba5c479..cedf943d6f 100644 --- a/src/Tags/Concerns/RendersForms.php +++ b/src/Tags/Concerns/RendersForms.php @@ -4,6 +4,7 @@ use Closure; use Illuminate\Support\MessageBag; +use Statamic\Fields\Field; use Statamic\Support\Str; trait RendersForms @@ -135,7 +136,12 @@ protected function getRenderableField($field, $errorBag = 'default', $manipulate $default = $field->value() ?? $field->defaultValue(); $value = $old === $missing ? $default : $old; - $data = array_merge($field->toArray(), [ + $configDefaults = Field::commonFieldOptions()->all() + ->merge($field->fieldtype()->configFields()->all()) + ->map->get('default') + ->filter()->all(); + + $data = array_merge($configDefaults, $field->toArray(), [ 'instructions' => $field->instructions(), 'error' => $errors->first($field->handle()) ?: null, 'default' => $field->value() ?? $field->defaultValue(), diff --git a/tests/Fields/FieldTest.php b/tests/Fields/FieldTest.php index dff1841765..edb0686ac9 100644 --- a/tests/Fields/FieldTest.php +++ b/tests/Fields/FieldTest.php @@ -462,62 +462,14 @@ public function defaultValue() } /** @test */ - public function it_converts_to_an_array() + public function converting_to_an_array_will_inline_the_handle() { - (new class extends Fieldtype - { - protected static $handle = 'test'; - protected $configFields = [ - 'baz' => [ - 'type' => 'text', - 'default' => 'qux', - ], - 'alfa' => [ - 'type' => 'text', - 'default' => 'bravo', - ], - 'toggle' => [ - 'type' => 'toggle', - ], - 'grid' => [ - 'type' => 'grid', - ], - ]; - })::register(); - - $field = new Field('the_handle', [ - 'type' => 'test', - 'foo' => 'bar', - 'replicator_preview' => false, - 'alfa' => 'charlie', - ]); + $field = new Field('the_handle', ['foo' => 'bar']); $this->assertEquals([ - // Handle and type are inlined. 'handle' => 'the_handle', - 'type' => 'test', - - // Width will always get merged in. + 'foo' => 'bar', 'width' => 100, - - // Explicitly set options. - 'foo' => 'bar', // Abitrary. - 'replicator_preview' => false, // A common field option. - 'alfa' => 'charlie', // A fieldtype option. - - // Defaults from common field options. - 'display' => null, - 'hide_display' => false, - 'instructions' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'visibility' => 'visible', - 'duplicate' => true, - - // Defaults from the fieldtype's config fields. - 'baz' => 'qux', - 'toggle' => false, - 'grid' => [], ], $field->toArray()); }