Page.create:after hook doesn't work as expected

I’ve been trying to get the hook to work for ages but I keep getting error messages. I know that Kirby objects are immutable but I can’t find a solution that works. Does anyone know what I’m doing wrong and how I should deal with the immutable objects? I have already cached them in variables but somehow something is missing … (Plugin for AddFields is active)

'page.create:after' => function ($page, $input) {
  if ( $page->intendedTemplate() == 'patient' ) {
    $kirby = kirby();
    try {
      $page->createChild([
        'slug' => 'nachrichten',
        'template' => 'nachrichten',
        'draft' => false,
        'content' => [
          'title'  => 'Nachrichten'
        ]
      ]);
    } catch (Exception $e) {
      echo $e->getMessage();
    }
    try {
      $page->update([
        'vorname' => $page->vorname(),
        'nachname' => $page->nachname()
      ]);
    } catch(Exception $e) {
      echo $e->getMessage();
    }
    $title = $page->nachname() . ' ' . $page->vorname();
    $slug = bin2hex(random_bytes(25));
    $new = $page->changeTitle($title);
    $new->changeSlug($slug);
  }
}
  1. The hook doesn’t have a second parameter $input, only $page

What the purpose of updating the page with data that already exists.

And you would have to store the result of this action in a new variable.

If you change the slug in a hook, you will not be redirected to the new location and get an error message in the Panel. Or do you get a different error message than “Page cannot be found”?

If you change the slug and title (or any other fields) in a page model (overwrite create() method) instead, you won’t run into this issue.

Hi Sonja,

Thank you very much. Yes, updating the page with the same data makes no sense, it was more of an experiment. But even without that I get the message that I am not authorised to change the page, see screenshot. Or do I have to work with Impersonate here? That I’m not redirected is ok, I know that cause of the AddFields Plugin.

Do you have an example for the page model?

You have to check your blueprint permissions

:exploding_head: I was not aware that the Blueprint has an effect on the hooks. Learned something again, thank you