Kirby

Set default slug for child pages

Hi all! In my project users have the option to create a single grand child page using Kirby’s standard pages section, like so:

    type: pages
    templates: glossary
    empty: No glossary yet
    info: "{{ page.parent.title }}"
    status: listed
    max: 1

So far so good. My question is how to set the slug of the newly created page to a default value (glossary) and ideally prevent it from being edited via the panel. The structure I’m after is this:

/apes/bonobo/glossary
/apes/gorilla/glossary

With normal fields this is no problem (default: 'glossary' and disabled: true ) but I’m not sure how to access the title and slug fields in the page creation dialog or if that is even possible.

You can override the $page->create() method in a model, see this solution: Hook page.create:before, how to edit user input?

You can then set the changeSlug option to false in your blueprint to prevent editing of the slug.

Thank you for the quick reply and helpful pointers. I have not worked with page models before so I may be barking up the wrong tree here. This is what I have so far (in models/glossary.php) and it doesn’t seem to do anything.

<?php

class glossaryPage extends Page {

    public static function create(array $props): Page {
        $props['title'] = 'Glossary';
        $props['slug'] = 'glossary';
        return $this->create($props);
    }

}

You have to to this in the parent page model

D’oh, thanks! That was pretty obvious.

After swapping
return $this->create($props);
for
return Page::create($props);
setting the slug like this works just fine but the title property seems to have no effect.

I ran into another problem with page creation. Setting a max: 1 limit works on a page section level but since I have a drafts section and a published section it means that once a page has been moved to ‘published’ the ‘Add Page’ button reappears and I can add another page. is there a simple workaround to prevent this?

Do you really need two sections if you only want to add one page max?

Not at all. Is there a way to use a single section for both drafts and published pages?

Yes, you can set status: all(and limit status to drafts and listed), all is the default anyway, so you don’t have to set this explicitly

Allowed statuses: https://getkirby.com/docs/reference/panel/blueprints/page#statuses

Fantastic! Thanks a lot for your relentless help!

1 Like