diff --git a/src/Structures/Tree.php b/src/Structures/Tree.php index 214179be77..208e11d4ba 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 728bf62285..32cc1f8edc 100644 --- a/tests/Data/Structures/CollectionTreeTest.php +++ b/tests/Data/Structures/CollectionTreeTest.php @@ -129,6 +129,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() {