Jump to content

append page id to page name


sanstraces
 Share

Recommended Posts

hallo!

i'm new to processwire. i try to append the page id to the page name with a hook, when the page is added. so it looks like page-id.

i used addHookAfter('added') for this, but its not working.

public function hookAfterAdded($event)
{
	$page = $event->arguments[0];
	$page->name = $page->name .'-'. $page->id;
	$this->message('added '.$page->name);
}

 

 any idea to solve the problem! Thanks

- sanstraces -

Link to comment
Share on other sites

I think the problem is that in the moment of adding the new page the ID ist not known yet.

So, the logical steps would be: add the page and then rename it. The issue has been discussed before.

  • Like 1
Link to comment
Share on other sites

Welcome @sanstraces

You need to save the page after you set the name.

public function hookAfterAdded($event)
{
    $page = $event->arguments[0];
    $page->name = $page->name .'-'. $page->id;
    $page->save();
    $this->message('added '.$page->name);
}

Normally the $page->setAndSave() method is useful in these situations, but there seems to be a bug when saving the page name.

 

 

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...