From 4194c41d7074895cc3e3d7426ca35b917f16ae3a Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Mon, 3 Jun 2024 18:44:52 +0100 Subject: [PATCH] [5.x] Merge config field defaults into field config (#10139) Co-authored-by: Jason Varga --- src/Fields/Fieldtype.php | 8 ++- .../Fieldtypes/MarkdownFieldtypeTest.php | 8 +-- .../GraphQL/Fieldtypes/CodeFieldtypeTest.php | 4 +- tests/Fields/FieldtypeTest.php | 61 +++++++++++++++++-- tests/Fieldtypes/MarkdownTest.php | 10 +-- 5 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/Fields/Fieldtype.php b/src/Fields/Fieldtype.php index 9c1bca046a..bdbc32fe3a 100644 --- a/src/Fields/Fieldtype.php +++ b/src/Fields/Fieldtype.php @@ -315,9 +315,13 @@ public function config(?string $key = null, $fallback = null) return $fallback; } + $config = $this->configFields()->all() + ->map->defaultValue() + ->merge($this->field->config()); + return $key - ? $this->field->get($key, $fallback) - : $this->field->config(); + ? ($config->get($key) ?? $fallback) + : $config->all(); } public function preload() 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; 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', 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 { // 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;