page.ChangeSlug:before returns null for the page object


I’m playing around with the page.ChangeSlug:before hook:

'page.changeSlug:before' => function(Kirby\Cms\Page $oldPage, string $newSlug, string $languageCode) {

    $oldSlug = $oldPage->slug();

However, when changing a slug to test the setup, I always get this error:

Error: Kirby\Cms\App::{closure}(): Argument #1 ($oldPage) must be of type Kirby\Cms\Page, null given

Why is the parameter passed for the page object null?



Hooks use named arguments since Kirby 3.4, and the variable names in your code don’t match those (looks like old code from pre-3.4). See page.changeSlug:before | Kirby CMS …i.e. should be function (Kirby\Cms\Page $page, string $slug, string $languageCode) instead.

So, the parameters names $page, $slug, and $languageCode cannot be changed. Is that correct?

Correct. That applies to all hooks. Since 3.4 the arguments are detected by name and no longer by order as before – they receive null if the variable name doesn’t match.

1 Like

Understood, thanks.

I copied the line from the documentation for page.changeSlug:before and tested again:

'page.changeSlug:before' => function (Kirby\Cms\Page $page, string $slug, string $languageCode) {

But now something appears to be wrong with the third argument:

Error: Kirby\Cms\App::{closure}(): Argument #3 ($languageCode) must be of type string, null given

Could it be that the documentation and the real code don’t match? Or did I make a mistake somewhere?

You’re right, should be like that:

'page.changeSlug:before' => function (Kirby\Cms\Page $page, string $slug, ?string $languageCode = null) { }

I’ve fixed the hook arguments:

1 Like

Now it’s working. Thanks a lot!