diff --git a/src/Entries/Entry.php b/src/Entries/Entry.php index 129b3bd7771..fc9a5156e6f 100644 --- a/src/Entries/Entry.php +++ b/src/Entries/Entry.php @@ -321,6 +321,10 @@ public function save() optional(Collection::findByMount($this))->updateEntryUris(); + if (!$isNew) { + $this->updateCacheOfLocalizedDescendants(); + } + foreach ($afterSaveCallbacks as $callback) { $callback($this); } @@ -344,6 +348,16 @@ public function save() return true; } + protected function updateCacheOfLocalizedDescendants() + { + $this->descendants()->each(function ($descendant) { + $collectionHandle = $this->collectionHandle(); + $store = app('stache')->store("entries::${collectionHandle}"); + $store->forgetItem($descendant->id()); + $store->getItem($descendant->id()); + }); + } + public function taxonomize() { Facades\Entry::taxonomize($this); diff --git a/tests/Data/Entries/EntryTest.php b/tests/Data/Entries/EntryTest.php index 22e2fe82a00..759eb6140c9 100644 --- a/tests/Data/Entries/EntryTest.php +++ b/tests/Data/Entries/EntryTest.php @@ -366,6 +366,57 @@ public function if_the_value_is_explicitly_set_to_null_then_it_should_not_fall_b $this->assertEquals('four in entry', $entry->value('four')); } + /** @test */ + public function it_updates_the_origin_of_descendants_when_saving_an_entry_with_localizations() + { + Facades\Site::setConfig([ + 'default' => 'en', + 'sites' => [ + 'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'], + 'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => 'http://fr.test.com/'], + 'de' => ['name' => 'German', 'locale' => 'de_DE', 'url' => 'http://test.com/de/'], + ], + ]); + + $collection = (new Collection) + ->handle('articles') + ->propagate(false) + ->sites(['en', 'fr', 'de']) + ->save(); + + $root = (new Entry) + ->id('en') + ->locale('en') + ->collection($collection) + ->data(['description' => 'Description from en']); + $root->save(); + + $localization = $root->makeLocalization('fr'); + $root->addLocalization($localization); // todo: necessary to call this because $entryEn::$localizations is already cached and does not get/recognize new localizations. should be called in Entry::makeLocalization() ? + $localization->save(); + + $deeperLocalization = $localization->makeLocalization('de'); + $localization->addLocalization($deeperLocalization); + $deeperLocalization->save(); + + $store = Facades\Stache::store('entries::articles'); + + // re-fetch root from cache to get a fresh object. + $store->forgetItem($root->id()); + $root = $store->getItem($root->id()); + + $this->assertEquals('Description from en', $store->getItem($localization->id())->value('description')); + $this->assertEquals('Description from en', $store->getItem($deeperLocalization->id())->value('description')); + + $store->forgetItem($root->id()); + $root = $store->getItem($root->id()); + + $root->data(['description' => 'Description from en modified'])->save(); + + $this->assertEquals('Description from en modified', $store->getItem($localization->id())->value('description')); + $this->assertEquals('Description from en modified', $store->getItem($deeperLocalization->id())->value('description')); + } + /** @test */ public function it_gets_custom_computed_data() {