Update URL when page is create in Multi lang environnement

Hi
How to automatically update the URL (for example add the date of today of the timestamp) when a page is create ? I tried with a Hook in the config.php (panel.page.create) but it does not work. Maybe because I’m in a multi lang environnement (3 langagues).
Does anyone can help us ? Thank you

Could you post your code please?

return [
'hooks' => [
    'page.create:before' => function ($page, $input) {
        $addToUrl = 'stringToAdd';
        $title = $page->title();
        $page->update(array(
            'URL-Key' => str::slug($title).$addToUrl,
        ));
        panel()->redirect($page, 'edit');
    }
]

You can’t update from a before hook, you have to use an after hook.

That is not valid code. Where did you get that from?

Ok for the after Hook thank you.
$page->update and ‘URL-Key’ is valid ?

No idea where panel()->redirect come from… Found it on the forum I think…
How to finish a Hook like this one the best way ?

You don’t have to anything after the updating. Note that there is no such thing as a URL-Key for the default language.

There is an alternative to using a hook though: A page model that overrides the create method.

You probably mixed that up with old Kirby 2 code. Always doublecheck the thread categories and if old example code is still valid.

Ok good thank you
So last question what is the parameter I have to update to change the URL ? If it’s not URL-key ?

The URL-Key is only useful for all non-default languages. Otherwise you would have to use the changeSlug() method. But note that if you change the slug in a hook, the user will not be redirected and get an error.

So changing the slug in a page model is the better option in this case.

class WhateverPage extends Page
{

    public static function create(array $props): Page
    {
        $props['slug'] = Str::slug('Hallo Welt');
        
        return parent::create($props);
    }
}
<?php
use Kirby\Cms\Page;
class EpisodePage extends Page
{
public function create(array $props): Page
{
    $props['slug'] = Str::slug('newUrl');

    return parent::create($props);
}
}

I tried like this but it looks like it does not affect the creation of the page (episode).

It must be public static function

Sorry but still not working with “static”

If you are on Kirby 3.4, you have to use the create method in the page model for tte page itself, not the parent. See also note here: Hook page.create:before, how to edit user input?