Skip to content

Commit a9f513b

Browse files
[5.x] Fix ensure field has config (#14195)
Co-authored-by: Jesse Leite <jesseleite@gmail.com>
1 parent e2482c1 commit a9f513b

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/Fields/Blueprint.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,11 @@ protected function ensureFieldInTabHasConfig($handle, $tab, $config)
645645
return $this;
646646
}
647647

648+
// If field is deferred as an ensured field, we'll need to update it instead
649+
if (! isset($fields[$handle]) && isset($this->ensuredFields[$handle])) {
650+
return $this->ensureEnsuredFieldHasConfig($handle, $config);
651+
}
652+
648653
$fieldKey = $fields[$handle]['fieldIndex'];
649654
$sectionKey = $fields[$handle]['sectionIndex'];
650655

@@ -664,6 +669,19 @@ protected function ensureFieldInTabHasConfig($handle, $tab, $config)
664669
return $this->resetBlueprintCache()->resetFieldsCache();
665670
}
666671

672+
private function ensureEnsuredFieldHasConfig($handle, $config)
673+
{
674+
if (! isset($this->ensuredFields[$handle])) {
675+
return $this;
676+
}
677+
678+
$existingConfig = Arr::get($this->ensuredFields[$handle], 'config', []);
679+
680+
$this->ensuredFields[$handle]['config'] = array_merge($existingConfig, $config);
681+
682+
return $this->resetBlueprintCache()->resetFieldsCache();
683+
}
684+
667685
public function validateUniqueHandles()
668686
{
669687
$fields = $this->fieldsCache ?? new Fields($this->tabs()->map->fields()->flatMap->items());

tests/Fields/BlueprintTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,41 @@ public function it_ensures_a_field_has_config()
898898

899899
// todo: duplicate or tweak above test but make the target field not in the first section.
900900

901+
#[Test]
902+
public function it_can_ensure_an_deferred_ensured_field_has_specific_config()
903+
{
904+
$blueprint = (new Blueprint)->setContents(['tabs' => [
905+
'tab_one' => [
906+
'sections' => [
907+
[
908+
'fields' => [
909+
['handle' => 'title', 'field' => ['type' => 'text']],
910+
],
911+
],
912+
],
913+
],
914+
]]);
915+
916+
// Let's say somewhere else in the code ensures an `author` field
917+
$blueprint->ensureField('author', ['type' => 'text', 'do_not_touch_other_config' => true, 'foo' => 'bar']);
918+
919+
// Then later, we try to ensure that `author` field has config, we should be able to successfully modify that deferred field
920+
$fields = $blueprint
921+
->ensureFieldHasConfig('author', ['foo' => 'baz', 'visibility' => 'read_only'])
922+
->fields();
923+
924+
$this->assertEquals(['type' => 'text'], $fields->get('title')->config());
925+
926+
$expectedConfig = [
927+
'type' => 'text',
928+
'do_not_touch_other_config' => true,
929+
'foo' => 'baz',
930+
'visibility' => 'read_only',
931+
];
932+
933+
$this->assertEquals($expectedConfig, $fields->get('author')->config());
934+
}
935+
901936
#[Test]
902937
public function it_merges_previously_undefined_keys_into_the_config_when_ensuring_a_field_exists_and_it_already_exists()
903938
{

0 commit comments

Comments
 (0)