From 0fbdd1618075209050890fb2c8cc7f410da4e094 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 21 May 2024 15:29:27 +0100 Subject: [PATCH 01/11] Merge config field defaults into field config --- src/Fields/Fieldtype.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Fields/Fieldtype.php b/src/Fields/Fieldtype.php index 9c1bca046a..260cdc758d 100644 --- a/src/Fields/Fieldtype.php +++ b/src/Fields/Fieldtype.php @@ -315,9 +315,14 @@ public function config(?string $key = null, $fallback = null) return $fallback; } + $config = $this->configFields()->all() + ->map->get('default') + ->filter() + ->merge($this->field->config()); + return $key - ? $this->field->get($key, $fallback) - : $this->field->config(); + ? $config->get($key, $fallback) + : $config->all(); } public function preload() From 158091e7eca08c4acbea9c71f2938cbc41e5a8da Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 21 May 2024 18:51:06 +0100 Subject: [PATCH 02/11] Tweak test assertions --- tests/Fieldtypes/MarkdownTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Fieldtypes/MarkdownTest.php b/tests/Fieldtypes/MarkdownTest.php index c291f9453e..eb63b61921 100644 --- a/tests/Fieldtypes/MarkdownTest.php +++ b/tests/Fieldtypes/MarkdownTest.php @@ -123,7 +123,7 @@ public function it_can_automatically_add_line_breaks_when_augmenting() EOT; $default = $this->fieldtype(); - $this->assertEqualsTrimmed($withoutBreaks, $default->augment($value)); + $this->assertEqualsTrimmed($withBreaks, $default->augment($value)); $enabled = $this->fieldtype(['automatic_line_breaks' => true]); $this->assertEqualsTrimmed($withBreaks, $enabled->augment($value)); @@ -156,12 +156,12 @@ public function it_converts_statamic_asset_urls() $expected = <<<'EOT'

Actual asset...

-

link - +

link
+
Asset

Non-existent asset...

-

link - +

link
+
Asset Link

EOT; From da00fc73e952cbece79d2f39ee7adbe6f7fed3b8 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 21 May 2024 18:57:45 +0100 Subject: [PATCH 03/11] fix runtime markdown tests --- .../Antlers/Runtime/Fieldtypes/MarkdownFieldtypeTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Antlers/Runtime/Fieldtypes/MarkdownFieldtypeTest.php b/tests/Antlers/Runtime/Fieldtypes/MarkdownFieldtypeTest.php index a668fdb99e..f79285321b 100644 --- a/tests/Antlers/Runtime/Fieldtypes/MarkdownFieldtypeTest.php +++ b/tests/Antlers/Runtime/Fieldtypes/MarkdownFieldtypeTest.php @@ -56,10 +56,10 @@ public function test_markdown_with_antlers_evaluates_correctly() $this->assertSame('

2019

', trim((string) $this->parser()->cascade($cascade)->render('{{ settings:field_name }}', $data))); $expected = <<<'EOT' -

No. -Yes. -3 is bigger -2019 +

No.
+Yes.
+3 is bigger
+2019
Just some content

EOT; From 00669d652dd6e1aa8846a6249db6d5e6eb875d8f Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 21 May 2024 19:02:46 +0100 Subject: [PATCH 04/11] `mode_selectable` defaults to true now --- tests/Feature/GraphQL/Fieldtypes/CodeFieldtypeTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/GraphQL/Fieldtypes/CodeFieldtypeTest.php b/tests/Feature/GraphQL/Fieldtypes/CodeFieldtypeTest.php index 74a49a7de9..138b80d5e5 100644 --- a/tests/Feature/GraphQL/Fieldtypes/CodeFieldtypeTest.php +++ b/tests/Feature/GraphQL/Fieldtypes/CodeFieldtypeTest.php @@ -11,11 +11,11 @@ public function it_gets_code() $this->createEntryWithFields([ 'filled' => [ 'value' => 'bar', - 'field' => ['type' => 'code'], + 'field' => ['type' => 'code', 'mode_selectable' => false], ], 'undefined' => [ 'value' => null, - 'field' => ['type' => 'code'], + 'field' => ['type' => 'code', 'mode_selectable' => false], ], 'selectable_string' => [ 'value' => 'bar', From 972567b21657cc62f2a5ebc9f258a464746426e7 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 28 May 2024 15:10:46 +0100 Subject: [PATCH 05/11] Merge common field defaults into `Field:: preProcessedConfig` Doing so will fix #10198. --- src/Fields/Field.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index a2f9445006..469312afdf 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -408,9 +408,14 @@ private function preProcessedConfig() $fields = $fieldtype->configFields()->addValues($this->config); - return array_merge($this->config, $fields->preProcess()->values()->all(), [ - 'component' => $fieldtype->component(), - ]); + return array_merge( + self::commonFieldOptions()->all()->map->get('default')->all(), + $this->config, + $fields->preProcess()->values()->all(), + [ + 'component' => $fieldtype->component(), + ] + ); } public function meta() From aae4aa3db22f6082f90986cef6b1955bf1dac035 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 28 May 2024 17:03:57 +0100 Subject: [PATCH 06/11] Expect common field defaults in tests --- tests/Fields/BlueprintTest.php | 20 ++++++++++++++++++++ tests/Fields/FieldTest.php | 5 +++++ tests/Fields/SectionTest.php | 10 ++++++++++ tests/Fields/TabTest.php | 10 ++++++++++ tests/Fieldtypes/SetsTest.php | 15 +++++++++++++++ 5 files changed, 60 insertions(+) diff --git a/tests/Fields/BlueprintTest.php b/tests/Fields/BlueprintTest.php index 9c1d228d5c..adba06bb3d 100644 --- a/tests/Fields/BlueprintTest.php +++ b/tests/Fields/BlueprintTest.php @@ -444,6 +444,11 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], ], @@ -472,6 +477,11 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], ], @@ -558,6 +568,11 @@ public function converts_to_array_suitable_for_rendering_prefixed_conditional_fi 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], [ 'handle' => 'nested_deeper_two', @@ -578,6 +593,11 @@ public function converts_to_array_suitable_for_rendering_prefixed_conditional_fi 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], ], diff --git a/tests/Fields/FieldTest.php b/tests/Fields/FieldTest.php index edb0686ac9..7f603a8bbc 100644 --- a/tests/Fields/FieldTest.php +++ b/tests/Fields/FieldTest.php @@ -339,6 +339,11 @@ public function preProcess($data) 'component' => 'example', 'a_config_field_with_pre_processing' => 'foo preprocessed', 'a_config_field_without_pre_processing' => 'foo', + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], $field->toPublishArray()); } diff --git a/tests/Fields/SectionTest.php b/tests/Fields/SectionTest.php index a3d6ea5cd5..2110abe4ac 100644 --- a/tests/Fields/SectionTest.php +++ b/tests/Fields/SectionTest.php @@ -133,6 +133,11 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], [ 'handle' => 'two', @@ -150,6 +155,11 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], ], $section->toPublishArray()); diff --git a/tests/Fields/TabTest.php b/tests/Fields/TabTest.php index 1611299dde..b757a3e479 100644 --- a/tests/Fields/TabTest.php +++ b/tests/Fields/TabTest.php @@ -158,6 +158,11 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], [ 'handle' => 'two', @@ -175,6 +180,11 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], ], diff --git a/tests/Fieldtypes/SetsTest.php b/tests/Fieldtypes/SetsTest.php index dcdf84eccb..c76d7b7510 100644 --- a/tests/Fieldtypes/SetsTest.php +++ b/tests/Fieldtypes/SetsTest.php @@ -233,6 +233,11 @@ public function it_preprocesses_for_config_with_groups() 'read_only' => false, 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], ], @@ -264,6 +269,11 @@ public function it_preprocesses_for_config_with_groups() 'read_only' => false, 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], ], @@ -318,6 +328,11 @@ public function it_preprocesses_for_config_without_groups() 'read_only' => false, 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], ], From f41625512c39dd081cc6fa44817f568fdf45c5f2 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 28 May 2024 17:13:06 +0100 Subject: [PATCH 07/11] Expect common field defaults in some other tests --- tests/Fields/FieldsTest.php | 20 ++++++++++++++++++++ tests/Fieldtypes/NestedFieldsTest.php | 13 +++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/tests/Fields/FieldsTest.php b/tests/Fields/FieldsTest.php index 554382afec..3d44a471de 100644 --- a/tests/Fields/FieldsTest.php +++ b/tests/Fields/FieldsTest.php @@ -427,6 +427,11 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], [ 'handle' => 'two', @@ -444,6 +449,11 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], $fields->toPublishArray()); } @@ -503,6 +513,11 @@ public function converts_to_array_suitable_for_rendering_prefixed_conditional_fi 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], [ 'handle' => 'nested_deeper_two', @@ -523,6 +538,11 @@ public function converts_to_array_suitable_for_rendering_prefixed_conditional_fi 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, + 'hide_display' => null, + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'replicator_preview' => true, + 'duplicate' => true, ], ], $fields->toPublishArray()); } diff --git a/tests/Fieldtypes/NestedFieldsTest.php b/tests/Fieldtypes/NestedFieldsTest.php index cd20b669cc..3b11f16706 100644 --- a/tests/Fieldtypes/NestedFieldsTest.php +++ b/tests/Fieldtypes/NestedFieldsTest.php @@ -71,18 +71,23 @@ public function preProcess($data) $this->assertSame([ [ + 'display' => 'Test Image Field', + 'hide_display' => null, + 'handle' => 'image', + 'instructions' => 'Some instructions', + 'instructions_position' => 'above', + 'listable' => 'hidden', + 'visibility' => 'visible', + 'replicator_preview' => true, + 'duplicate' => true, 'type' => 'assets', 'max_files' => 2, 'container' => 'main', 'foo' => 'bar', - 'display' => 'Test Image Field', - 'instructions' => 'Some instructions', 'validate' => 'required', 'component' => 'assets', - 'handle' => 'image', 'prefix' => null, 'required' => true, - 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, ], From ce2247cfaf7880409cc1c27f5da5e522dd5d9732 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 31 May 2024 15:10:08 -0400 Subject: [PATCH 08/11] revert stuff thatll go in a separate pr --- src/Fields/Field.php | 11 +++-------- tests/Fields/BlueprintTest.php | 20 -------------------- tests/Fields/FieldTest.php | 5 ----- tests/Fields/FieldsTest.php | 20 -------------------- tests/Fields/SectionTest.php | 10 ---------- tests/Fields/TabTest.php | 10 ---------- tests/Fieldtypes/NestedFieldsTest.php | 13 ++++--------- tests/Fieldtypes/SetsTest.php | 15 --------------- 8 files changed, 7 insertions(+), 97 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 469312afdf..a2f9445006 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -408,14 +408,9 @@ private function preProcessedConfig() $fields = $fieldtype->configFields()->addValues($this->config); - return array_merge( - self::commonFieldOptions()->all()->map->get('default')->all(), - $this->config, - $fields->preProcess()->values()->all(), - [ - 'component' => $fieldtype->component(), - ] - ); + return array_merge($this->config, $fields->preProcess()->values()->all(), [ + 'component' => $fieldtype->component(), + ]); } public function meta() diff --git a/tests/Fields/BlueprintTest.php b/tests/Fields/BlueprintTest.php index cfc670f097..14bd932cb5 100644 --- a/tests/Fields/BlueprintTest.php +++ b/tests/Fields/BlueprintTest.php @@ -444,11 +444,6 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], ], @@ -477,11 +472,6 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], ], @@ -568,11 +558,6 @@ public function converts_to_array_suitable_for_rendering_prefixed_conditional_fi 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], [ 'handle' => 'nested_deeper_two', @@ -593,11 +578,6 @@ public function converts_to_array_suitable_for_rendering_prefixed_conditional_fi 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], ], diff --git a/tests/Fields/FieldTest.php b/tests/Fields/FieldTest.php index 7f603a8bbc..edb0686ac9 100644 --- a/tests/Fields/FieldTest.php +++ b/tests/Fields/FieldTest.php @@ -339,11 +339,6 @@ public function preProcess($data) 'component' => 'example', 'a_config_field_with_pre_processing' => 'foo preprocessed', 'a_config_field_without_pre_processing' => 'foo', - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], $field->toPublishArray()); } diff --git a/tests/Fields/FieldsTest.php b/tests/Fields/FieldsTest.php index 3d44a471de..554382afec 100644 --- a/tests/Fields/FieldsTest.php +++ b/tests/Fields/FieldsTest.php @@ -427,11 +427,6 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], [ 'handle' => 'two', @@ -449,11 +444,6 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], $fields->toPublishArray()); } @@ -513,11 +503,6 @@ public function converts_to_array_suitable_for_rendering_prefixed_conditional_fi 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], [ 'handle' => 'nested_deeper_two', @@ -538,11 +523,6 @@ public function converts_to_array_suitable_for_rendering_prefixed_conditional_fi 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], $fields->toPublishArray()); } diff --git a/tests/Fields/SectionTest.php b/tests/Fields/SectionTest.php index 2110abe4ac..a3d6ea5cd5 100644 --- a/tests/Fields/SectionTest.php +++ b/tests/Fields/SectionTest.php @@ -133,11 +133,6 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], [ 'handle' => 'two', @@ -155,11 +150,6 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], ], $section->toPublishArray()); diff --git a/tests/Fields/TabTest.php b/tests/Fields/TabTest.php index b757a3e479..1611299dde 100644 --- a/tests/Fields/TabTest.php +++ b/tests/Fields/TabTest.php @@ -158,11 +158,6 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'read_only' => false, // deprecated 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], [ 'handle' => 'two', @@ -180,11 +175,6 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], ], diff --git a/tests/Fieldtypes/NestedFieldsTest.php b/tests/Fieldtypes/NestedFieldsTest.php index 3b11f16706..cd20b669cc 100644 --- a/tests/Fieldtypes/NestedFieldsTest.php +++ b/tests/Fieldtypes/NestedFieldsTest.php @@ -71,23 +71,18 @@ public function preProcess($data) $this->assertSame([ [ - 'display' => 'Test Image Field', - 'hide_display' => null, - 'handle' => 'image', - 'instructions' => 'Some instructions', - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'visibility' => 'visible', - 'replicator_preview' => true, - 'duplicate' => true, 'type' => 'assets', 'max_files' => 2, 'container' => 'main', 'foo' => 'bar', + 'display' => 'Test Image Field', + 'instructions' => 'Some instructions', 'validate' => 'required', 'component' => 'assets', + 'handle' => 'image', 'prefix' => null, 'required' => true, + 'visibility' => 'visible', 'read_only' => false, // deprecated 'always_save' => false, ], diff --git a/tests/Fieldtypes/SetsTest.php b/tests/Fieldtypes/SetsTest.php index c76d7b7510..dcdf84eccb 100644 --- a/tests/Fieldtypes/SetsTest.php +++ b/tests/Fieldtypes/SetsTest.php @@ -233,11 +233,6 @@ public function it_preprocesses_for_config_with_groups() 'read_only' => false, 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], ], @@ -269,11 +264,6 @@ public function it_preprocesses_for_config_with_groups() 'read_only' => false, 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], ], @@ -328,11 +318,6 @@ public function it_preprocesses_for_config_without_groups() 'read_only' => false, 'always_save' => false, 'autocomplete' => null, - 'hide_display' => null, - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'duplicate' => true, ], ], ], From ce4e460b2ec892b3f14e5903535d3e78955be490 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 31 May 2024 15:52:21 -0400 Subject: [PATCH 09/11] Add a test that shows that it gets default values falling back to whats defined in the fieldtype, and for different types. --- tests/Fields/FieldtypeTest.php | 61 +++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/tests/Fields/FieldtypeTest.php b/tests/Fields/FieldtypeTest.php index 838e848485..ad29ccf042 100644 --- a/tests/Fields/FieldtypeTest.php +++ b/tests/Fields/FieldtypeTest.php @@ -501,17 +501,35 @@ public function no_pre_processing_happens_by_default_for_the_index() public function it_gets_a_config_value() { $field = new Field('test', [ - 'foo' => 'bar', - 'baz' => 'qux', + 'foo' => 'bar', // doesn't exist as a config field + 'alfa' => 'overridden', // doesn't have a default + 'bravo' => 'also overridden', // does have a default ]); - $fieldtype = (new TestFieldtype)->setField($field); + $fieldtype = (new TestFieldtypeWithConfigFields)->setField($field); $this->assertEquals([ 'foo' => 'bar', - 'baz' => 'qux', + 'alfa' => 'overridden', + 'bravo' => 'also overridden', + 'charlie' => 'charlie!', + // Toggle fields (has default of boolean false) + 'delta' => false, // No default set + 'echo' => true, // Default set + 'foxtrot' => false, // Default set + // Files fields (has default of empty array) + 'golf' => [], // No default set + 'hotel' => ['hotel!'], // Default set ], $fieldtype->config()); $this->assertEquals('bar', $fieldtype->config('foo')); + $this->assertEquals('overridden', $fieldtype->config('alfa')); + $this->assertEquals('also overridden', $fieldtype->config('bravo')); + $this->assertEquals('charlie!', $fieldtype->config('charlie')); + $this->assertEquals(false, $fieldtype->config('delta')); + $this->assertEquals(true, $fieldtype->config('echo')); + $this->assertEquals(false, $fieldtype->config('foxtrot')); + $this->assertEquals([], $fieldtype->config('golf')); + $this->assertEquals(['hotel!'], $fieldtype->config('hotel')); $this->assertNull($fieldtype->config('unknown')); $this->assertEquals('fallback', $fieldtype->config('unknown', 'fallback')); } @@ -551,6 +569,41 @@ class TestFieldtype extends Fieldtype // } +class TestFieldtypeWithConfigFields extends Fieldtype +{ + protected $configFields = [ + 'alfa' => [ + 'type' => 'text', + ], + 'bravo' => [ + 'type' => 'text', + 'default' => 'bravo!', + ], + 'charlie' => [ + 'type' => 'text', + 'default' => 'charlie!', + ], + 'delta' => [ + 'type' => 'toggle', + ], + 'echo' => [ + 'type' => 'toggle', + 'default' => true, + ], + 'foxtrot' => [ + 'type' => 'toggle', + 'default' => false, + ], + 'golf' => [ + 'type' => 'files', + ], + 'hotel' => [ + 'type' => 'files', + 'default' => ['hotel!'], + ], + ]; +} + class TestMultiWordFieldtype extends Fieldtype { // From 5f67f17258c6fb53b7c69bfd2fed2ebf445a5ebd Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 31 May 2024 15:53:57 -0400 Subject: [PATCH 10/11] grab from the fieldtype's defaultValue if set, and dont filter them out. --- src/Fields/Fieldtype.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Fields/Fieldtype.php b/src/Fields/Fieldtype.php index 260cdc758d..16316f4c12 100644 --- a/src/Fields/Fieldtype.php +++ b/src/Fields/Fieldtype.php @@ -316,8 +316,7 @@ public function config(?string $key = null, $fallback = null) } $config = $this->configFields()->all() - ->map->get('default') - ->filter() + ->map->defaultValue() ->merge($this->field->config()); return $key From 08254039efac79459d591647d2d1bd392cc63488 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 31 May 2024 15:58:38 -0400 Subject: [PATCH 11/11] since the array will have nulls and empty arrays etc, null coalesce instead. --- src/Fields/Fieldtype.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fields/Fieldtype.php b/src/Fields/Fieldtype.php index 16316f4c12..bdbc32fe3a 100644 --- a/src/Fields/Fieldtype.php +++ b/src/Fields/Fieldtype.php @@ -320,7 +320,7 @@ public function config(?string $key = null, $fallback = null) ->merge($this->field->config()); return $key - ? $config->get($key, $fallback) + ? ($config->get($key) ?? $fallback) : $config->all(); }