From d2d8ed4e69b278291c73437b03f4e7727b71dbde Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Wed, 25 Jun 2025 10:43:56 -0400 Subject: [PATCH] Backports "[6.x] Fix ensure field has config (#11898)" --- src/Fields/Blueprint.php | 18 +++++++++++++++++ tests/Fields/BlueprintTest.php | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index c1e3b14c217..616f2e4fec0 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -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']; @@ -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()); diff --git a/tests/Fields/BlueprintTest.php b/tests/Fields/BlueprintTest.php index 2be10d841da..5ef21d4f39c 100644 --- a/tests/Fields/BlueprintTest.php +++ b/tests/Fields/BlueprintTest.php @@ -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() {