Hook page.create:before, how to edit user input?

Yes, this works. @lukaskleinschmidt, @texnixe thank you very much for the quick help.

Note: In Kirby 3.4.0, this code must be connect to the Page, not Parents. I only changed the file name in the folder (site/models) and then it works again.

Can you help me with the code of the version for Kirby 3.4 ? I don’t see how to modifie this one for the actual page model and not the parent model… My template and blueprint is call “episode.php” and “episode.yml”. I would like to update the URL of a episode when is created.

It should look something like this:

<?php

// site/models/episode.php

class EpisodePage extends Page
{
    /**
     * Creates and stores a new page
     *
     * @param array $props
     * @return self
     */
    public static function create(array $props)
    {
        $props['slug'] = 'my-new-slug';
        
        return parent::create($props);
    }
}
2 Likes

Fantastic @lukaskleinschmidt ! It works perfectly !

Great!
Thank you again for you original solution and the fix! <3

Thank you very much! I’ve got my old page hook working again :slight_smile:

I tried to add another function in case the user updates the page. The slug should always be generated.
Being new to page models I’m not sure how to do that. Could you give me a hint?

Thanks again!

I have not tested it but I think this should work. I also added the changeTitle method as this might be useful too here.

<?php

class CustomPage extends Page
{
    /**
     * Change the page title
     *
     * @param string $title
     * @param string|null $languageCode
     * @return static
     */
    public function changeTitle(string $title, string $languageCode = null)
    {
        $page = parent::changeTitle($title, $languageCode);

        return $page->changeSlug('my-new-slug', $languageCode);
    }

    /**
     * Creates and stores a new page
     *
     * @param array $props
     * @return static
     */
    public static function create(array $props)
    {
        $props['slug'] = 'my-new-slug';
        
        return parent::create($props);
    }

    /**
     * Updates the page data
     *
     * @param array|null $input
     * @param string|null $languageCode
     * @param bool $validate
     * @return static
     */
    public function update(array $input = null, string $languageCode = null, bool $validate = false)
    {
        $page = parent::update($input, $languageCode, $validate);

        return $page->changeSlug('my-new-slug', $languageCode);
    }
}

Thank you!

The update function throws this error: Cannot make non static method Kirby\Cms\Page::update() static in class EventPage. When I remove the static the slug change works. Unfortunately it has the same problem as the hooks (and the changeTitle function), when they’re used to change the slug: They don’t redirect the user to the page with the new slug and throw an error, which is not ideal.

Is there a way to redirect to the parent page after the change?

Thats too bad. And for what I can tell you are probably out of luck here for now. Perhaps this is possile with the new Fiber backend at some point. At least there is a method that could work but in a quick test I could not get it working in this context.

That’s what I thought. Well I’ll have a look what happens with 3.6 in the next weeks and months.

I appreciate your help!