Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Merge config field defaults into field config #10139

Merged
merged 12 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Fields/Fieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions tests/Antlers/Runtime/Fieldtypes/MarkdownFieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public function test_markdown_with_antlers_evaluates_correctly()
$this->assertSame('<p>2019</p>', trim((string) $this->parser()->cascade($cascade)->render('{{ settings:field_name }}', $data)));

$expected = <<<'EOT'
<p>No.
Yes.
3 is bigger
2019
<p>No.<br />
Yes.<br />
3 is bigger<br />
2019<br />
Just some content</p>
EOT;

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/GraphQL/Fieldtypes/CodeFieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
61 changes: 57 additions & 4 deletions tests/Fields/FieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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
{
//
Expand Down
10 changes: 5 additions & 5 deletions tests/Fieldtypes/MarkdownTest.php
jasonvarga marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -156,12 +156,12 @@ public function it_converts_statamic_asset_urls()

$expected = <<<'EOT'
<h1>Actual asset...</h1>
<p><a href="/assets/foo/hoff.jpg">link</a>
<img src="/assets/foo/hoff.jpg" alt="" />
<p><a href="/assets/foo/hoff.jpg">link</a><br />
<img src="/assets/foo/hoff.jpg" alt="" /><br />
<img src="/assets/foo/hoff.jpg" alt="Asset" /></p>
<h1>Non-existent asset...</h1>
<p><a href="">link</a>
<img src="" alt="" />
<p><a href="">link</a><br />
<img src="" alt="" /><br />
<a href="">Asset Link</a></p>

EOT;
Expand Down
Loading