Tino Posted September 7, 2022 Share Posted September 7, 2022 Hi @all, The headline is saying everything and I guess it is quite simple but I'm just diving into all of this. In this case I want to modify the title to be a combination of two other fields. Thank you very much Link to comment Share on other sites More sharing options...
szabesz Posted September 7, 2022 Share Posted September 7, 2022 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. 3 Link to comment Share on other sites More sharing options...
Tino Posted September 7, 2022 Author Share Posted September 7, 2022 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. 2 Link to comment Share on other sites More sharing options...
bernhard Posted September 8, 2022 Share Posted September 8, 2022 9 hours ago, Tino said: 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. That is a good concern ? 3 1 Link to comment Share on other sites More sharing options...
Tino Posted September 8, 2022 Author Share Posted September 8, 2022 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 ? 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now