'Subpage builder' for pages already created

I’ve implemented the ‘Subpage builder’ (Subpage builder | Kirby CMS) and it works great. Is there any way I can get it to work on pages that already exist? Sadly duplicating the pages also doesn’t work, and recreating them all from scratch would be the very last resort.

I’ve work out a solution to temporarily add this hook:

            'page.update:after' => function ($newPage, $oldPage) {
            buildPageTree($newPage);
        }
    ],

Then remove the hook manually once all of the subpages are added to avoid an error. Maybe there’s a better way, so I’ll keep to topic here.

Hint:
I would add a check in 'page.update:after', whether the subpages do not yet exist

1 Like

I’d create the pages via script instead of waiting until the update hook kicks in.

Filter the collection by whether or not the subpages already exist, then loop through the remaining pages and call the buildPageTree() method on them.

2 Likes

For the modules plugin i used a route:after hook which works great. (Thanks @nilshoerrmann for the tip)

This way you don’t have to update the page, just open it.

Here’s the full code I use: https://github.com/medienbaecker/kirby-modules/blob/master/index.php#L60-L87

2 Likes