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

Hi, I’d like to edit @lukaskleinschmidt solution so that the new slug will be “myTitle + thisYear.”

But I can’t get the filed title() from modal. Where am I making mistake? Thank you for your help.

Not tested but it should probably look something like this.

class ParentPage extends Page
{
    /**
     * Creates and stores a new page
     *
     * @param array $props
     * @return Page
     */
    public static function create(array $props): Page
    {
        $props['slug'] = $props['content']['title'] . '-' . date('Y');
        
        return parent::create($props);
    }
}

Note:

Since Kirby 3.4 you need to overwrite the create method on the actual page model that is going to be created. Not the parent model!

2 Likes

Thank you for the quick reply.

But for your solution, the panel writes the error: “Undefined index: title”.

I’m thinking, can modal give this value?

Just checked and it has to be $props['content']['title'] instead of $props['title']. I updated the answer above.

Great! Now it really works. @lukaskleinschmidt thank you very much for your help.

Hi everyone,

I updated my site from Kirby 3.3.6 to the new version 3.4.0 and then this Custom page model from @lukaskleinschmidt stopped working.

I think it could be related to a change of Hooks or new $props arguments in the new Kirby version. But I can’t fix this code.

Can you advise me where I am making a mistake? Thank you advance.

Nope, that code should still work. It has nothing to do with the hooks. Do you get an error or just not the desired result? Are you using this in conjunction with a plugin?

This doesn’t print any error. Only the command is not executed. When I go back to version 3.3.6, then the code works again. The file is only in the site/models folder. S plugins it isn’t connect.

Just had a look at this. It is related to this commit. So instead of overwriting the create method on the parent model you now have to do this on the actual page model that is going to be created.

@texnixe could you please add a info to my post above like:

Since Kirby 3.4 you need to overwrite the create method on the actual page model that is going to be created. Not the parent model!

Ah, ok, that explains why it works in my environment.

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!