MarkE Posted April 2, 2019 Share Posted April 2, 2019 I've googled a fair bit but can't find anywhere an API method to move a page to be under a new parent. Obviously this would have to handle naming conflicts. Is there one? Or a module? Or just a code snippet ? Thanks Link to comment Share on other sites More sharing options...
elabx Posted April 2, 2019 Share Posted April 2, 2019 Try this: $page = $pages->get('name=page-to-move'); $page->of(false); $page->parent = $pages->get('name=new-parent'); $page->save(); Link to comment Share on other sites More sharing options...
MarkE Posted April 2, 2019 Author Share Posted April 2, 2019 Thanks - that's effectively what I was doing, except in a before Pages::save hook, thus: $p = $event->arguments(0); //..some code here ..// $p->parent = pages()->get('/news/'); $event->arguments(0, $p); It works fine so long as the name does not already exist under the new parent. I seem to recall that the core uses a routine to add -1, -2 etc. but I can't find it. Link to comment Share on other sites More sharing options...
elabx Posted April 2, 2019 Share Posted April 2, 2019 Had forgotten about it also!! Hope you are up to this version of PW: https://processwire.com/blog/posts/pw-3.0.111/ Maybe this would work? //In the context of the hook $p->name = $this->pages->names->uniquePageName($this->sanitizer->pageName($p->title)); 2 Link to comment Share on other sites More sharing options...
MarkE Posted April 3, 2019 Author Share Posted April 3, 2019 Thanks - that's what I was looking for. Here is my final code for the hook: $p->name = $this->pages->names()->uniquePageName($p->name); $p->parent = pages()->get('/news/'); $event->arguments(0, $p); Works a treat! 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now