sanstraces

append page id to page name

5 posts in this topic

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 -

Share this post


Link to post
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.

1 person likes this

Share this post


Link to post
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.

 

 

1 person likes this

Share this post


Link to post
Share on other sites

@sanstraces - depending on your actual needs, this might also be an option: 

 

1 person likes this

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.