Move a page to a location based on its content

Hi,

I have a page with a select field within it, that looks like this:

settingType:
  type: select
  label: Setting type
  default: primary
  empty: false
  width: 1/3
  options:
    primary: Primary
    secondary: Secondary
    university: University
    alternative: Alternative provision

I need to set up a hook that checks when the page is saved, and if the contents of this field has changed, to move the page to a new location based on this. I have pages with slugs that match the values of the select, but am struggling to get this to work. This is what I have in my hook:

'page.update:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
  if($newPage->intendedTemplate() == 'setting'):
    if($newPage->settingType() !== $oldPage->settingType()):
      $newPage->move(???) // This needs to be the new parent page using the value of settingType e.g. 'university'
    endif;
  endif;
},

I can’t get the move to work as I would like. It needs to move the page to a parent with the slug the value of the select field, but I am not sure what to put inside the move here.

If anyone has any thoughts that would be super.

Expects the target parent page as page object (or the site object, but not relevant in your case)

So get the target page from the settingType value, then pass that to move()

If you move a page via hook, I think the Panel user will see an error because the page at the current URL will not exist anymore.

Thanks!

That was the nudge I needed, sorted it with this:

$newParent = page($newPage->settingType());
$newPage->move($newParent);

I get your point about the error - is there a better way to handle this? It’s a bit of an edge case (a school mis-identifying its type during signup) but want to address it.