From fd70ebf76f3f997e5c55ba4ebaf2bf21915a1815 Mon Sep 17 00:00:00 2001 From: CapitaineToinon Date: Thu, 25 Jun 2026 22:49:25 +0200 Subject: [PATCH 1/2] Added saveQuietly, deleteQuietly and tests --- src/Structures/Tree.php | 31 ++++++++++++++++++-- tests/Data/Structures/CollectionTreeTest.php | 17 +++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Structures/Tree.php b/src/Structures/Tree.php index 214179be772..208e11d4ba4 100644 --- a/src/Structures/Tree.php +++ b/src/Structures/Tree.php @@ -26,6 +26,7 @@ abstract class Tree implements ContainsQueryableValues, Contract, Localization protected $cachedFlattenedPagesByReference; protected $cachedFlattenedPageOrder; protected $withEntries = false; + protected $withEvents = true; protected $uriCacheEnabled = true; public function idKey() @@ -161,7 +162,10 @@ public function entryOrder($reference) public function save() { - if ($this->dispatchSavingEvent() === false) { + $withEvents = $this->withEvents; + $this->withEvents = true; + + if ($withEvents && $this->dispatchSavingEvent() === false) { return false; } @@ -174,24 +178,45 @@ public function save() $this->repository()->save($this); - $this->dispatchSavedEvent(); + if ($withEvents) { + $this->dispatchSavedEvent(); + } $this->syncOriginal(); return true; } + public function saveQuietly() + { + $this->withEvents = false; + + return $this->save(); + } + public function delete() { + $withEvents = $this->withEvents; + $this->withEvents = true; + Blink::forget('collection-structure-tree*'); $this->repository()->delete($this); - $this->dispatchDeletedEvent(); + if ($withEvents) { + $this->dispatchDeletedEvent(); + } return true; } + public function deleteQuietly() + { + $this->withEvents = false; + + return $this->delete(); + } + abstract protected function repository(); protected function dispatchSavedEvent() diff --git a/tests/Data/Structures/CollectionTreeTest.php b/tests/Data/Structures/CollectionTreeTest.php index 728bf622855..662586e99ab 100644 --- a/tests/Data/Structures/CollectionTreeTest.php +++ b/tests/Data/Structures/CollectionTreeTest.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\Event; use PHPUnit\Framework\Attributes\Test; use Statamic\Events\CollectionTreeEntriesMovedOrRemoved; +use Statamic\Events\CollectionTreeSaved; use Statamic\Events\CollectionTreeSaving; use Statamic\Facades\Blink; use Statamic\Facades\Collection; @@ -129,6 +130,22 @@ public function it_fires_a_saving_event() $this->assertFileExists($tree->path()); } + #[Test] + public function it_does_not_fire_a_saving_event_when_saving_quietly() + { + Event::fake(); + + $collection = Collection::make('test')->structureContents(['root' => true]); + Collection::shouldReceive('findByHandle')->with('test')->andReturn($collection); + + $tree = $collection->structure()->makeTree('en'); + $tree->saveQuietly(); + + Event::assertNotDispatched(CollectionTreeSaving::class); + + $this->assertFileExists($tree->path()); + } + #[Test] public function returning_false_in_collection_tree_saving_stops_saving() { From 4db453362e1c1c8f6ae4c349b38082e9032ea436 Mon Sep 17 00:00:00 2001 From: CapitaineToinon Date: Thu, 25 Jun 2026 23:17:30 +0200 Subject: [PATCH 2/2] Removed unused import --- tests/Data/Structures/CollectionTreeTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Data/Structures/CollectionTreeTest.php b/tests/Data/Structures/CollectionTreeTest.php index 662586e99ab..32cc1f8edc3 100644 --- a/tests/Data/Structures/CollectionTreeTest.php +++ b/tests/Data/Structures/CollectionTreeTest.php @@ -5,7 +5,6 @@ use Illuminate\Support\Facades\Event; use PHPUnit\Framework\Attributes\Test; use Statamic\Events\CollectionTreeEntriesMovedOrRemoved; -use Statamic\Events\CollectionTreeSaved; use Statamic\Events\CollectionTreeSaving; use Statamic\Facades\Blink; use Statamic\Facades\Collection;