Jump to content

How to programmatically change page name after saving it?


VeiJari
 Share

Recommended Posts

Hello forum!

I've yet again stumbled on a head-scratching situation. We have enabled the option on our articles template and events template that it skips the title adding part and goes straight to the form. This is what our customer wants. So when you add a new article or event it automatically names it temporary to "article-0000000" and same with event. Now the problem is that obviously after saving the form we want to change to page url or "name" to the title, like it's normally. 

Now here's the code for the hook:

wire()->addHookBefore("Pages::saved(template=tapahtuma|artikkeli)", function($hook) {
  $page = $hook->arguments(0);
  $newUrl = wire()->sanitizer->pageName($page->title); // give it a name used in the url for the page
  wire()->log->message($page->name);
  $page->setAndSave('name', $newUrl);
});

I get the correct page and the name and path changes when I log them, but when I try to save it. It just loads and then I get: 

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) This happens in sanitizer.php

and then another error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0

 

What is happening? Am I not suppose to use sanitizer in this way? When we made a temporary page object in out other hook, the sanitizer worked perfectly.

Thanks for the help!

Link to comment
Share on other sites

You're creating an infinite loop. You hook into "after save" change something and save again, which again triggers your "after save" hook and so on. Either look for another hook to do your work (potentially change things last minute before the actual save) or add some way for your hook to only run once and not for the saves it initiated on it's own.

  • Like 2
Link to comment
Share on other sites

@VeiJari maybe hook after pages::saveReady is better for this. But also it is possible in a pages::saved hook to save the page again without running into an infinite loop with, for example, using a temporary property:

wire()->addHookBefore("Pages::saved(template=tapahtuma|artikkeli)", function($hook) {
	$page = $hook->arguments(0);
	if($page->skipMyHook) {             // if the page already has our temporary property, we skip further processing
		return;
	}
	$newUrl = wire()->sanitizer->pageName($page->title); // give it a name used in the url for the page
	wire()->log->message($page->name);
	$page->skipMyHook = true;           // add a temporary property to the page (it is only in memory)
	$page->setAndSave('name', $newUrl);
});

 

  • Like 1
Link to comment
Share on other sites

2 hours ago, LostKobrakai said:

You're creating an infinite loop. You hook into "after save" change something and save again, which again triggers your "after save" hook and so on. Either look for another hook to do your work (potentially change things last minute before the actual save) or add some way for your hook to only run once and not for the saves it initiated on it's own.

Ah, silly me ? Of course I didn't think the obvious.

It now works and I'm using this module: https://modules.processwire.com/modules/page-rename-options/ for all the pages at the moment!

Thanks anyway!

37 minutes ago, horst said:

@VeiJari maybe hook after pages::saveReady is better for this. But also it is possible in a pages::saved hook to save the page again without running into an infinite loop with, for example, using a temporary property:


wire()->addHookBefore("Pages::saved(template=tapahtuma|artikkeli)", function($hook) {
	$page = $hook->arguments(0);
	if($page->skipMyHook) {             // if the page already has our temporary property, we skip further processing
		return;
	}
	$newUrl = wire()->sanitizer->pageName($page->title); // give it a name used in the url for the page
	wire()->log->message($page->name);
	$page->skipMyHook = true;           // add a temporary property to the page (it is only in memory)
	$page->setAndSave('name', $newUrl);
});

 

Ah I see! Good to know, might be handy someday for preventing those pesty infinite loops ?

Thanks horst

  • 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

×
×
  • Create New...