From bd3b176e2b9f54dd51f793a2783e4df91ba506c6 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Mon, 13 May 2024 20:31:47 -0400 Subject: [PATCH 1/2] Add failing test --- tests/Fields/BlueprintTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Fields/BlueprintTest.php b/tests/Fields/BlueprintTest.php index 804dc7bbc8f..9c1d228d5cc 100644 --- a/tests/Fields/BlueprintTest.php +++ b/tests/Fields/BlueprintTest.php @@ -11,6 +11,7 @@ use Statamic\Contracts\Query\QueryableValue; use Statamic\CP\Column; use Statamic\CP\Columns; +use Statamic\Entries\Entry; use Statamic\Events\BlueprintCreated; use Statamic\Events\BlueprintCreating; use Statamic\Events\BlueprintDeleted; @@ -783,6 +784,29 @@ public function it_ensures_a_field_exists_in_a_specific_tab() $this->assertEquals(['type' => 'textarea'], $blueprint->fields()->get('new')->config()); } + /** @test */ + public function it_can_add_fields_multiple_times() + { + $blueprint = (new Blueprint) + ->setNamespace('collections/collection_one') + ->setHandle('blueprint_one'); + + $entry = (new Entry) + ->collection('collection_one') + ->blueprint($blueprint); + + $blueprint->setParent($entry); + + $blueprint->ensureFieldsInTab(['field_one' => ['type' => 'text']], 'tab_one'); + + $this->assertTrue($blueprint->hasField('field_one')); + + $blueprint->ensureField('field_two', ['type' => 'textarea']); + + $this->assertTrue($blueprint->hasField('field_two')); + + } + /** @test */ public function it_ensures_a_field_has_config() { From 38b670350abb265641e4c9fec9785ba9ea3929c6 Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 18 May 2024 16:04:18 -0500 Subject: [PATCH 2/2] Clear cached blueprint handle when mutating fields --- src/Fields/Blueprint.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index 2e77a4ec8ee..78791e045a3 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -514,7 +514,7 @@ public function ensureFieldInTab($handle, $config, $tab, $prepend = false) $this->ensuredFields[$handle] = compact('handle', 'tab', 'prepend', 'config'); - $this->resetFieldsCache(); + $this->resetBlueprintCache()->resetFieldsCache(); return $this; } @@ -573,7 +573,7 @@ public function removeTab($handle) Arr::pull($this->contents['tabs'], $handle); - return $this->resetFieldsCache(); + return $this->resetBlueprintCache()->resetFieldsCache(); } public function removeFieldFromTab($handle, $tab) @@ -591,7 +591,7 @@ public function removeFieldFromTab($handle, $tab) // Pull it out. Arr::pull($this->contents['tabs'][$tab]['sections'][$sectionIndex]['fields'], $fieldKey); - return $this->resetFieldsCache(); + return $this->resetBlueprintCache()->resetFieldsCache(); } private function getTabFields($tab) @@ -621,7 +621,7 @@ protected function ensureFieldInTabHasConfig($handle, $tab, $config) // Merge in new field config. $this->contents['tabs'][$tab]['sections'][$sectionKey]['fields'][$fieldKey]['field'] = array_merge($existingConfig, $config); - return $this->resetFieldsCache(); + return $this->resetBlueprintCache()->resetFieldsCache(); } public function validateUniqueHandles() @@ -635,6 +635,13 @@ public function validateUniqueHandles() } } + protected function resetBlueprintCache() + { + $this->lastBlueprintHandle = null; + + return $this; + } + protected function resetFieldsCache() { if ($this->parent) {