diff --git a/src/Entries/Entry.php b/src/Entries/Entry.php index 0b158daeda..24838c8cd4 100644 --- a/src/Entries/Entry.php +++ b/src/Entries/Entry.php @@ -32,7 +32,7 @@ use Statamic\Facades\Collection; use Statamic\Facades\Site; use Statamic\Facades\Stache; -use Statamic\Fields\Value; +use Statamic\Facades\User; use Statamic\GraphQL\ResolvesValues; use Statamic\Revisions\Revisable; use Statamic\Routing\Routable; @@ -340,6 +340,27 @@ public function save() }); } + if (! $isNew && $this->collection()->dated()) { + $date = $this->date(); + + $this->descendants()->each(function ($entry) use ($date) { + if ($entry->date()->eq($date)) { + return; + } + + $entry->date($date); + + if ($entry->revisionsEnabled() && $entry->published()) { + $entry + ->makeWorkingCopy() + ->user(User::current()) + ->save(); + } else { + $entry->updateLastModified(User::current())->save(); + } + }); + } + return true; } diff --git a/tests/Data/Entries/EntryTest.php b/tests/Data/Entries/EntryTest.php index 805a1d0517..aaf009bd10 100644 --- a/tests/Data/Entries/EntryTest.php +++ b/tests/Data/Entries/EntryTest.php @@ -847,6 +847,23 @@ public function it_gets_and_sets_the_date_for_date_collections() $this->assertTrue($dateEntry->hasTime()); } + /** @test */ + public function it_copies_a_changed_date_to_its_descendants() + { + $collection = tap(Collection::make('dated')->dated(true))->save(); + + $origin = (new Entry)->collection('dated')->id('origin')->date('2022-09-06'); + $origin->save(); + $descendant = (new Entry)->collection('dated')->id('descendant')->date('2022-09-06')->origin($origin); + $descendant->save(); + + $this->assertTrue(Carbon::createFromFormat('Y-m-d H:i', '2022-09-06 00:00')->eq($descendant->date())); + + $origin->date('2022-09-08')->save(); + + $this->assertTrue(Carbon::createFromFormat('Y-m-d H:i', '2022-09-08 00:00')->eq($descendant->date())); + } + /** @test */ public function future_dated_entries_are_private_when_configured_in_the_collection() {