Jump to content

How to hook before page saving (creating/editing) just at a specific template?


Tino
 Share

Recommended Posts

Hello,

In sort, you can do something like this in a saveReady hook:

if($page->template == 'yourtemplate') {...

It is also  possible to use a "shortcut", like this:

$wire->addHookBefore("Pages::saveReady(template=yourtemplate)", function($event) {...

https://processwire.com/docs/modules/hooks/

Quote: Some hookable methods in ProcessWire have no implementation and exist purely for you to hook. Because hooks like this exist purely "to be hooked", it doesn't matter if you hook before or after them. Examples include Pages::saveReady which is called after it's been determined the page will definitely be saved, but right before it is done. Another is Pages::saved, which is called after a page has successfully been saved. These hooks provide more certainty and less need for error checking than hooking before or after Pages::save. You may see several other specific-purpose hooks like this in ProcessWire.

Ryan in a pro module support thread provided this general example for repeaters, for example:

Quote: A Pages::saveReady hook is your best bet for populating the value of some field on a page before it is saved. If you wanted to populate some page reference field on a repeater item when it is saved, you could do so like this in your /site/ready.php file. In my example below, I'm populating some_page_field with the page that owns the repeater item, since I think this is what you were trying to do (?). You'd want to update the first 2 lines to reflect your actual repeater template name and page reference field name.

$pages->addHookBefore('Pages::saveReady', function(HookEvent $event) {
  $templateName = 'repeater_template'; 
  $fieldName = 'some_page_field'; 
  $page = $event->arguments(0);
  if($page->template == $templateName && $page instanceof RepeaterPage) { 
    $ownerPage = $page->getForPage();
    $page->set($fieldName, $ownerPage); 
  }
});

More examples:

https://processwire.com/talk/topic/26897-add-new-set-title-of-new-page-with-a-select-field-combination-pagereference/?do=findComment&comment=222469

Modifying field values is easy, but if you need to do it based on the values of other fields then things might get "tricky" sometimes, depending on the types of those fields.

 

 

  • Like 3
Link to comment
Share on other sites

WOW!

Thank you so much for taking the time and putting that effort together!

That did the job perfect.

$wire->addHookBefore("Pages::saveReady(template=guest)", function($event) {
    $page = $event->arguments(0);
    $pages = $event->object;
    // Set the title
	$page->title = "{$page->firstname} {$page->lastname}";
	// Sanitize the title as a page name
	$name = $event->wire()->sanitizer->pageName($page->title, true);
	// Set the page name while making sure it auto-increments if there is a sibling page with the same title
	$page->name = $pages->names()->uniquePageName($name, $page);

});

My only concern is if I put it in the /site/ready.php and have some more stuff like that things get messy, but for now it is perfect.

  • Like 2
Link to comment
Share on other sites

Hy berhard,

thank you very much for the video! It‘s awsome how quick everybody is here ?

And you gave lot‘s of other useful information and put everything together in a very good way, good job!

Danke dir vielmals ?

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...