Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Update child URIs when parent slug changes #9454

Merged
merged 8 commits into from May 8, 2024
Merged
38 changes: 38 additions & 0 deletions src/Entries/UpdateStructuredEntryChildUris.php
@@ -0,0 +1,38 @@
<?php

namespace Statamic\Entries;

use Statamic\Events\EntrySaved;

class UpdateStructuredEntryChildUris
{
public function handle(EntrySaved $event)
{
$entry = $event->entry;
$collection = $entry->collection();

// If it's orderable (single depth structure) then changing the
// position of the entries is never going to affect the uris.
if ($collection->orderable()) {
return;
}

// If the collection has no route, there are no uris to update.
if (! $collection->route($entry->locale())) {
return;
}

// If there's no page, there are no children to update.
if (! $page = $entry->page()) {
return;
}

$ids = $page->flattenedPages()->pluck('id');

if (empty($ids)) {
return;
}

$collection->updateEntryUris($ids);
}
}
3 changes: 3 additions & 0 deletions src/Providers/EventServiceProvider.php
Expand Up @@ -20,6 +20,9 @@ class EventServiceProvider extends ServiceProvider
\Statamic\Entries\UpdateStructuredEntryUris::class,
\Statamic\Entries\UpdateStructuredEntryOrder::class,
],
\Statamic\Events\EntrySaved::class => [
\Statamic\Entries\UpdateStructuredEntryChildUris::class,
],
\Statamic\Events\EntryBlueprintFound::class => [
\Statamic\Entries\AddSiteColumnToBlueprint::class,
],
Expand Down