Jump to content

Change page name to custom field values on save


a-ok
 Share

Recommended Posts

I'm attempting to change the page name of a page, when it is saved, using the API. Below is my hook. The message appears (as a test) but the change never happens. Any thoughts?

 function myHook(HookEvent $event) {
 	$page = $event->arguments(0);
 	if ($page->template->name == 'work-detail') {
		if ($page->global_text) {
			$pageName =  wire()->sanitizer->pageName($page->title . '-' . $page->global_text, true);
		} else {
			$pageName =  wire()->sanitizer->pageName($page->title, true);
		}
		wire()->message($pageName);
		$page->setOutputFormatting(false);
		$page->setAndSave('name', $pageName);
 	}
 }
 wire()->addHookAfter('Pages::save', null, 'myHook'); // On page SAVE run this hook

 

Link to comment
Share on other sites

4 minutes ago, bernhard said:

I don't know it right now but maybe the page name is reset after that hook. Does it work using Pages::saved instead of ::save? I'm usually using saveReady or saved to hook ? 

Thanks for the reply! It does not, sadly. Grrrr.

Link to comment
Share on other sites

Try this

if ($this->page->template == 'admin') {

	function customPageName(HookEvent $event) {
		$dummy = 'hook-generated';
	 	$page = $event->arguments(0);
	 	if ($page->template->name == 'basic-page') {
			$pageName =  wire()->sanitizer->pageName($page->title . '-' . $dummy, true);
			$page->setOutputFormatting(false);
			$page->name = $pageName;
	 	}
	 }

	 $this->addHookAfter('Pages::saveReady', null, 'customPageName');

};

I guess a certain combination of hook + save() will create an infinite loop, but this (abbreviated) example works just fine here.

Link to comment
Share on other sites

10 minutes ago, dragan said:

Try this


if ($this->page->template == 'admin') {

	function customPageName(HookEvent $event) {
		$dummy = 'hook-generated';
	 	$page = $event->arguments(0);
	 	if ($page->template->name == 'basic-page') {
			$pageName =  wire()->sanitizer->pageName($page->title . '-' . $dummy, true);
			$page->setOutputFormatting(false);
			$page->name = $pageName;
	 	}
	 }

	 $this->addHookAfter('Pages::saveReady', null, 'customPageName');

};

I guess a certain combination of hook + save() will create an infinite loop, but this (abbreviated) example works just fine here.

Many thanks for this. I haven't tried it yet but what's the fundamental difference here? The "$page->name =" rather than "setAndSave"?

Link to comment
Share on other sites

I have followed @bernhard's suggestion and hooked into saveReady, and replaced setAndSave() with just $page->name = $pageName.

Because... a hook named saveReady doesn't need a separate (injected) save() command. You can SET values right before it SAVES things.

  • Like 2
Link to comment
Share on other sites

5 minutes ago, dragan said:

I have followed @bernhard's suggestion and hooked into saveReady, and replaced setAndSave() with just $page->name = $pageName.

Because... a hook named saveReady doesn't need a separate (injected) save() command. You can SET values right before it SAVES things.

Brilliant. That does work. Why would the first code work, but not the second?

if ($this->page->template == 'admin') {

	function customPageName(HookEvent $event) {
		$dummy = 'hook-generated';
	 	$page = $event->arguments(0);
	 	if ($page->template->name == 'work-detail') {
			if ($page->global_text) {
				$pageName = wire()->sanitizer->pageName($page->title . '-' . $page->global_text, true);
			} else {
				$pageName = wire()->sanitizer->pageName($page->title, true);
			}
			$page->setOutputFormatting(false);
			$page->name = $pageName;
	 	}
	 }

	 $this->addHookAfter('Pages::saveReady', null, 'customPageName');

};

 

function myHook(HookEvent $event) {
	$page = $event->arguments(0);
	if ($page->template->name == 'work-detail') {
		if ($page->global_text) {
			$pageName = wire()->sanitizer->pageName($page->title . '-' . $page->global_text, true);
		} else {
			$pageName = wire()->sanitizer->pageName($page->title, true);
		}
		$page->setOutputFormatting(false);
		$page->name = $pageName;
	}
}
wire()->addHookAfter('Pages::saveReady', null, 'myHook');

 

Link to comment
Share on other sites

try changing

20 minutes ago, a-ok said:

wire()->addHookAfter('Pages::saveReady', null, 'myHook');

 

to

20 minutes ago, a-ok said:

$this->addHookAfter('Pages::saveReady', null, 'customPageName');

 

Link to comment
Share on other sites

Now I'm confused. That didn't work but that made it almost 100% like your example. I commented mine out and put back yours and it worked.

Bizarre.

I even put wire()-> back in instead of $this and it still worked – maybe a hidden glyph or something?

Link to comment
Share on other sites

44 minutes ago, dragan said:

yeah, since a recent forum update, copy-and-pasting code snippets can wield weird results ?

Ahh. Your example was fine though; it was my original that wasn't. Who knows. Hate that! Thanks for all the help, you too @bernhard

  • Like 2
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...