Skip to content

Commit

Permalink
[5.x] Merge defaults into field publish arrays (#10246)
Browse files Browse the repository at this point in the history
Co-authored-by: Duncan McClean <[email protected]>
  • Loading branch information
jasonvarga and duncanmcclean authored Jun 3, 2024
1 parent 4194c41 commit 588a1ed
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 111 deletions.
11 changes: 8 additions & 3 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->defaultValue()->all(),
$this->config,
$fields->preProcess()->values()->all(),
[
'component' => $fieldtype->component(),
]
);
}

public function meta()
Expand Down
96 changes: 58 additions & 38 deletions tests/Fields/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,63 +413,73 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo
],
]);

$this->assertEquals([
$this->assertSame([
'title' => 'Test',
'handle' => 'test',
'tabs' => [
[
'display' => 'Tab one',
'handle' => 'tab_one',
'instructions' => 'Does stuff',
'handle' => 'tab_one',
'sections' => [
[
'fields' => [
[
'handle' => 'one',
'prefix' => null,
'type' => 'text',
'display' => 'One',
'hide_display' => false,
'handle' => 'one',
'instructions' => 'One instructions',
'required' => true,
'instructions_position' => 'above',
'listable' => 'hidden',
'visibility' => 'visible',
'replicator_preview' => true,
'duplicate' => true,
'type' => 'text',
'validate' => 'required|min:2',
'component' => 'text',
'input_type' => 'text',
'placeholder' => null,
'default' => null,
'character_limit' => 0,
'input_type' => 'text',
'autocomplete' => null,
'prepend' => null,
'append' => null,
'antlers' => false,
'default' => null,
'visibility' => 'visible',
'component' => 'text',
'prefix' => null,
'required' => true,
'read_only' => false, // deprecated
'always_save' => false,
'autocomplete' => null,
],
],
],
],
],
[
'display' => 'Tab two',
'handle' => 'tab_two',
'instructions' => null,
'handle' => 'tab_two',
'sections' => [
[
'fields' => [
[
'handle' => 'two',
'prefix' => null,
'type' => 'textarea',
'display' => 'Two',
'hide_display' => false,
'handle' => 'two',
'instructions' => 'Two instructions',
'required' => false,
'validate' => 'min:2',
'instructions_position' => 'above',
'listable' => 'hidden',
'visibility' => 'visible',
'replicator_preview' => true,
'duplicate' => true,
'type' => 'textarea',
'placeholder' => null,
'validate' => 'min:2',
'character_limit' => null,
'component' => 'textarea',
'antlers' => false,
'default' => null,
'visibility' => 'visible',
'antlers' => false,
'component' => 'textarea',
'prefix' => null,
'required' => false,
'read_only' => false, // deprecated
'always_save' => false,
],
Expand Down Expand Up @@ -528,56 +538,66 @@ public function converts_to_array_suitable_for_rendering_prefixed_conditional_fi
],
]);

$this->assertEquals([
$this->assertSame([
'title' => 'Test',
'handle' => 'test',
'tabs' => [
[
'display' => 'Tab one',
'handle' => 'tab_one',
'instructions' => null,
'handle' => 'tab_one',
'sections' => [
[
'fields' => [
[
'display' => 'Nested One',
'hide_display' => false,
'handle' => 'nested_one',
'prefix' => 'nested_',
'instructions' => null,
'instructions_position' => 'above',
'listable' => 'hidden',
'visibility' => 'visible',
'replicator_preview' => true,
'duplicate' => true,
'type' => 'text',
'display' => 'Nested One',
'placeholder' => null,
'input_type' => 'text',
'placeholder' => null,
'default' => null,
'character_limit' => 0,
'autocomplete' => null,
'prepend' => null,
'append' => null,
'antlers' => false,
'component' => 'text',
'instructions' => null,
'prefix' => 'nested_',
'required' => false,
'antlers' => false,
'default' => null,
'visibility' => 'visible',
'read_only' => false, // deprecated
'always_save' => false,
'autocomplete' => null,
],
[
'display' => 'Nested Deeper Two',
'hide_display' => false,
'handle' => 'nested_deeper_two',
'prefix' => 'nested_deeper_',
'instructions' => null,
'instructions_position' => 'above',
'listable' => 'hidden',
'visibility' => 'visible',
'replicator_preview' => true,
'duplicate' => true,
'type' => 'text',
'display' => 'Nested Deeper Two',
'placeholder' => null,
'input_type' => 'text',
'placeholder' => null,
'default' => null,
'character_limit' => 0,
'autocomplete' => null,
'prepend' => null,
'append' => null,
'antlers' => false,
'component' => 'text',
'instructions' => null,
'prefix' => 'nested_deeper_',
'required' => false,
'antlers' => false,
'default' => null,
'visibility' => 'visible',
'read_only' => false, // deprecated
'always_save' => false,
'autocomplete' => null,
],
],
],
Expand Down
28 changes: 19 additions & 9 deletions tests/Fields/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ public function it_adds_nullable_rule_when_not_required()
/** @test */
public function converts_to_array_suitable_for_rendering_fields_in_publish_component()
{
FieldtypeRepository::partialMock();

FieldtypeRepository::shouldReceive('find')
->with('example')
->andReturn(new class extends Fieldtype
Expand Down Expand Up @@ -320,25 +322,33 @@ public function preProcess($data)
'type' => 'example',
'display' => 'Test Field',
'instructions' => 'Test instructions',
'instructions_position' => 'below',
'validate' => 'required',
'foo' => 'bar',
'a_config_field_with_pre_processing' => 'foo',
'a_config_field_without_pre_processing' => 'foo',
]);

$this->assertEquals([
'handle' => 'test',
'prefix' => null,
'type' => 'example',
$this->assertSame([
'display' => 'Test Field',
'hide_display' => false,
'handle' => 'test',
'instructions' => 'Test instructions',
'required' => true,
'validate' => 'required',
'instructions_position' => 'below',
'listable' => 'hidden',
'visibility' => 'visible',
'read_only' => false, // deprecated
'always_save' => false,
'component' => 'example',
'replicator_preview' => true,
'duplicate' => true,
'type' => 'example',
'validate' => 'required',
'foo' => 'bar',
'a_config_field_with_pre_processing' => 'foo preprocessed',
'a_config_field_without_pre_processing' => 'foo',
'component' => 'example',
'prefix' => null,
'required' => true,
'read_only' => false, // deprecated
'always_save' => false,
], $field->toPublishArray());
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Fields/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => false,
'instructions_position' => 'above',
'listable' => 'hidden',
'replicator_preview' => true,
'duplicate' => true,
],
[
'handle' => 'two',
Expand All @@ -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' => false,
'instructions_position' => 'above',
'listable' => 'hidden',
'replicator_preview' => true,
'duplicate' => true,
],
], $fields->toPublishArray());
}
Expand Down Expand Up @@ -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' => false,
'instructions_position' => 'above',
'listable' => 'hidden',
'replicator_preview' => true,
'duplicate' => true,
],
[
'handle' => 'nested_deeper_two',
Expand All @@ -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' => false,
'instructions_position' => 'above',
'listable' => 'hidden',
'replicator_preview' => true,
'duplicate' => true,
],
], $fields->toPublishArray());
}
Expand Down
46 changes: 28 additions & 18 deletions tests/Fields/SectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,45 +109,55 @@ public function converts_to_array_suitable_for_rendering_fields_in_publish_compo
],
]);

$this->assertEquals([
$this->assertSame([
'display' => 'Test Section',
'instructions' => 'Does stuff',
'fields' => [
[
'handle' => 'one',
'prefix' => null,
'type' => 'text',
'display' => 'One',
'hide_display' => false,
'handle' => 'one',
'instructions' => 'One instructions',
'required' => true,
'instructions_position' => 'above',
'listable' => 'hidden',
'visibility' => 'visible',
'replicator_preview' => true,
'duplicate' => true,
'type' => 'text',
'validate' => 'required|min:2',
'component' => 'text',
'placeholder' => null,
'input_type' => 'text',
'placeholder' => null,
'default' => null,
'character_limit' => 0,
'autocomplete' => null,
'prepend' => null,
'append' => null,
'antlers' => false,
'default' => null,
'visibility' => 'visible',
'component' => 'text',
'prefix' => null,
'required' => true,
'read_only' => false, // deprecated
'always_save' => false,
'autocomplete' => null,
],
[
'handle' => 'two',
'prefix' => null,
'type' => 'textarea',
'display' => 'Two',
'hide_display' => false,
'handle' => 'two',
'instructions' => 'Two instructions',
'required' => false,
'instructions_position' => 'above',
'listable' => 'hidden',
'visibility' => 'visible',
'replicator_preview' => true,
'duplicate' => true,
'type' => 'textarea',
'validate' => 'min:2',
'character_limit' => null,
'component' => 'textarea',
'antlers' => false,
'placeholder' => null,
'character_limit' => null,
'default' => null,
'visibility' => 'visible',
'antlers' => false,
'component' => 'textarea',
'prefix' => null,
'required' => false,
'read_only' => false, // deprecated
'always_save' => false,
],
Expand Down
Loading

0 comments on commit 588a1ed

Please sign in to comment.