Jump to content

Recommended Posts

Posted

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

 

Posted

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 ? 

Posted
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.

Posted

Bit of an update. Using TraceyDebugger it looks as if the name does change (so to speak), it even says so in the session, but it isn't updated on the backend or the frontend.

Screen Shot 2019-07-04 at 17.24.54.jpg

Posted

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.

Posted
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"?

Posted

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
Posted
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');

 

Posted

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');

 

Posted

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?

Posted
12 minutes ago, a-ok said:

maybe a hidden glyph or something?

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

  • Like 1
Posted
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

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.
×
×
  • Create New...