Skip to content
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
18 changes: 18 additions & 0 deletions src/Fields/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ protected function ensureFieldInTabHasConfig($handle, $tab, $config)
return $this;
}

// If field is deferred as an ensured field, we'll need to update it instead
if (! isset($fields[$handle]) && isset($this->ensuredFields[$handle])) {
return $this->ensureEnsuredFieldHasConfig($handle, $config);
}

$fieldKey = $fields[$handle]['fieldIndex'];
$sectionKey = $fields[$handle]['sectionIndex'];

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

private function ensureEnsuredFieldHasConfig($handle, $config)
{
if (! isset($this->ensuredFields[$handle])) {
return $this;
}

$existingConfig = Arr::get($this->ensuredFields[$handle], 'config', []);

$this->ensuredFields[$handle]['config'] = array_merge($existingConfig, $config);

return $this->resetBlueprintCache()->resetFieldsCache();
}

public function validateUniqueHandles()
{
$fields = $this->fieldsCache ?? new Fields($this->tabs()->map->fields()->flatMap->items());
Expand Down
35 changes: 35 additions & 0 deletions tests/Fields/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,41 @@ public function it_ensures_a_field_has_config()

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

#[Test]
public function it_can_ensure_an_deferred_ensured_field_has_specific_config()
{
$blueprint = (new Blueprint)->setContents(['tabs' => [
'tab_one' => [
'sections' => [
[
'fields' => [
['handle' => 'title', 'field' => ['type' => 'text']],
],
],
],
],
]]);

// Let's say somewhere else in the code ensures an `author` field
$blueprint->ensureField('author', ['type' => 'text', 'do_not_touch_other_config' => true, 'foo' => 'bar']);

// Then later, we try to ensure that `author` field has config, we should be able to successfully modify that deferred field
$fields = $blueprint
->ensureFieldHasConfig('author', ['foo' => 'baz', 'visibility' => 'read_only'])
->fields();

$this->assertEquals(['type' => 'text'], $fields->get('title')->config());

$expectedConfig = [
'type' => 'text',
'do_not_touch_other_config' => true,
'foo' => 'baz',
'visibility' => 'read_only',
];

$this->assertEquals($expectedConfig, $fields->get('author')->config());
}

#[Test]
public function it_merges_previously_undefined_keys_into_the_config_when_ensuring_a_field_exists_and_it_already_exists()
{
Expand Down
Loading