From 60a88bc83014686eef68f78f49bfe01a0741c776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20R=C3=BCtter?= Date: Thu, 30 Jul 2026 08:26:46 +0200 Subject: [PATCH] ensure fields into the first section of a tab instead of the last Co-Authored-By: Claude Fable 5 --- src/Fields/Blueprint.php | 4 +-- tests/Fields/BlueprintTest.php | 59 +++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index 2503a80c277..1ad89742eb0 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -281,8 +281,7 @@ private function addEnsuredFieldToContents($contents, $ensured) } } - $targetSectionIndex = $existingField['section'] - ?? ($prepend ? 0 : count($contents['tabs'][$tab]['sections'] ?? []) - 1); + $targetSectionIndex = $existingField['section'] ?? 0; $fields = collect($tabs[$tab]['sections'][$targetSectionIndex]['fields'] ?? [])->keyBy(function ($field) { return (isset($field['import'])) ? 'import:'.($field['prefix'] ?? null).$field['import'] : $field['handle']; @@ -293,6 +292,7 @@ private function addEnsuredFieldToContents($contents, $ensured) $importKey = 'import:'.$importedField['partial']; $field = $allFields->get($importKey); $tab = $field['tab']; + $targetSectionIndex = $field['section']; $fields = collect($tabs[$tab]['sections'][$targetSectionIndex]['fields'])->keyBy(function ($field) { return (isset($field['import'])) ? 'import:'.($field['prefix'] ?? null).$field['import'] : $field['handle']; }); diff --git a/tests/Fields/BlueprintTest.php b/tests/Fields/BlueprintTest.php index e9e4232f41c..9e9095af2b5 100644 --- a/tests/Fields/BlueprintTest.php +++ b/tests/Fields/BlueprintTest.php @@ -783,12 +783,12 @@ public function it_ensures_a_field_exists() [ 'fields' => [ ['handle' => 'existing_in_section_one', 'field' => ['type' => 'text']], + ['handle' => 'new', 'field' => ['type' => 'textarea']], ], ], [ 'fields' => [ ['handle' => 'existing_in_section_two', 'field' => ['type' => 'text']], - ['handle' => 'new', 'field' => ['type' => 'textarea']], ], ], ], @@ -1203,6 +1203,63 @@ public function it_merges_undefined_config_overrides_when_ensuring_a_field_that_ $this->assertEquals(['type' => 'text', 'foo' => 'bar'], $blueprint->fields()->get('one')->config()); } + #[Test] + public function it_merges_config_overrides_when_ensuring_a_field_inside_an_imported_fieldset_in_a_later_section() + { + FieldsetRepository::shouldReceive('find')->with('the_partial')->andReturn( + (new Fieldset)->setContents(['fields' => [ + [ + 'handle' => 'one', + 'field' => ['type' => 'text'], + ], + ]]) + ); + + $blueprint = (new Blueprint)->setContents(['tabs' => [ + 'tab_one' => [ + 'sections' => [ + [ + 'fields' => [ + ['handle' => 'existing', 'field' => ['type' => 'text']], + ], + ], + [ + 'fields' => [ + ['import' => 'the_partial'], + ], + ], + ], + ], + ]]); + + $return = $blueprint->ensureField('one', ['type' => 'textarea', 'foo' => 'bar']); + + $this->assertEquals($blueprint, $return); + $this->assertTrue($blueprint->hasField('one')); + $this->assertEquals(['tabs' => [ + 'tab_one' => [ + 'sections' => [ + [ + 'fields' => [ + ['handle' => 'existing', 'field' => ['type' => 'text']], + ], + ], + [ + 'fields' => [ + [ + 'import' => 'the_partial', + 'config' => [ + 'one' => ['foo' => 'bar'], + ], + ], + ], + ], + ], + ], + ]], $blueprint->contents()); + $this->assertEquals(['type' => 'text', 'foo' => 'bar'], $blueprint->fields()->get('one')->config()); + } + #[Test] public function it_ensures_a_field_exists_if_it_doesnt_and_prepends_it() {