Automatically update page URL when title is edited?

Hmm ok. I am not in a multi language environment. I am managing to change the URL with the code below, however once it is saved, instead of redirecting the user to the page which has been moved (e.g. panel/pages/home/some-project/edit, it takes them one level back to panel/pages/home/edit ?

kirby()->hook('panel.page.update', function($page) {
  try {
    $url = str::slug($page->title());
    $page->move($url);
    panel()->redirect('pages/' . $page->id() . '/edit');
  } catch(Exception $e) {
    echo $e->getMessage();
  }
  return;
});

This feels a little sketchy UX wise, so I’m considering just sending an alert when the title field is edited, only problem is I’m not there is a hook available for specific fields? I see you have responded to a similar question here however using the hidden field to check the value against didn’t work for me:

kirby()->hook('panel.page.update', function($page) {
  $title = $page->title();
  if ($page->content()->hasField('hidden')) {
    $titleHistory = $page->hidden();
  } else {
    $titleHistory = "";
  }
  if ($title !== $titleHistory) {
    $page->update(array(
      'hidden' => $title,
    ));
    panel()->alert("WARNING! If you have changed the title, do not forget to update the URL! Go to Change URL > 'Create from title'");
    panel()->redirect('pages/' . $page->id() . '/edit');
  }
});

Thanks for your time btw, much appreciated.

1 Like