Jump to content
VeiJari

How to programmatically change page name after saving it?

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!

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Hardoman
      Hello community,
      we have a website running version 3.0.118. The owner would like to have a watermark merged to the images, that are being uploaded in the CKEditor as a requirement.
      Image upload besides the CKEditor within galleries and single images works as a charm already. We also use croppable image 3 there. (PIM2)
      To realize this requirement, I thought of using a hook in the admin area. So, I read a lot in our forums and tested this by adding a hook into the ready.php file.
      $this->addHookAfter('InputfieldFile::fileAdded',function(HookEvent$event){ wire('log')->save('test','Image upload works'); ... The log entry is being created correctly. But when I try to use the pim/watermark-function like in a template, he cannot find the watermark-image anymore. Furthermore, when I try to get the page-id, it does not seem to be accessible, because the application does not seem to know how to reference it, or I dont know the right way to do so…
      So my questions are:
       
      Is this the right attempt at all or will there be another, better workaround? It seems, I cannot access the page object (of the content page) within this scope or file but I would need it to save the processed image inside the right files/id folder Would it be better to place the hook into the admin-template? (or admin.php)
        Thanks for any hints in advance. 🙂
    • By VeiJari
      Hello forum, this is my first security related post, so I'm a bit of a newbie.
      I understand that when I have direct front-input from user I should sanitize the input, but how about when I use a secret key for showing a API for a third-party supplier? Should I sanitize the input->get() key?
      I've tested this issue and I tried ?key=<?php echo $page->field; ?> And without adding any sanitization it comes back: /?key=<?php%20echo%20$page->field;%20?>
      So can I rely on this, or should I still use $sanitizer just in case?
       
      Thanks for the help!
    • By EyeDentify
      I have been experimenting with the new $page->meta() method and find it useful.

      Once i figured out that the data i "save" with it is tied to the page where i called the method from.

      So this is not obvious at least not for me in the documentation:
      https://processwire.com/api/ref/page/meta/
       
      So i just wanted to share that revelation with the community so you don´t get as confused as i was.

      Happy Coding Everyone.
    • By stanoliver
      In the new page-meta-method (https://processwire.com/blog/posts/pw-3.0.133/#comments) there is an example how to ouput "world". 
      Could may someone give me a snippet of code so that the output is one of the colors (red, green, blue).
×
×
  • Create New...