Christophe Posted February 22, 2018 Share Posted February 22, 2018 Hello, How to automatically change a page's parent then its template once/after the page is published (while being a child of a temporary parent)? The new template is only available for a child of the new parent (and the old template is also available). Does someone know how to do this? It's the first thing I have to find a solution for. To translate the following to a hook in ready.php, or in another more adequate file? If a page using the directory_page_temporary template (or perhaps it's better to say if a page whose parent has the name annuaire-repertoire-temporaire-avant-validation, or is using the directory_listing_temporary template) is set to published: if the page's qui_1 (radio button) field is set to mannequin-modele (page reference), then: change its parent to/move it under the parent whose name is mannequins-modeles (or who is using the template directory_listing_mm), change its template to directory_page_mm. if the page's qui_1 (radio button) field is set to autre-artiste (page reference), then: move it under the parent whose name is autres-artistes (or who is using the template directory_listing_autres_artistes), change its template to directory_page_autre_artiste. qui_1 is of type Page Reference. In the Field tab, in Selectable pages, Parent is set to Choix Mannequin/Modèle ou Autre Artiste (Choix translating to Choice). This Choix Mannequin/Modèle ou Autre Artiste page's path is /fr/outils/choix-mannequin-modele-ou-autre-artiste/ (the website is multilingual-ready, but only French is currently activated/used). And Choix Mannequin/Modèle ou Autre Artiste's children are, as you have probably guessed, Mannequin/Modèle (mannequin-modele being the name) and Autre Artiste (autre-artiste being the name).Outils and its children are Hidden. Edit: the page should be automatically added to the top under its new parent. Thanks in advance for any help! [ Related topic: https://processwire.com/talk/topic/18497-help-needed-to-improve-a-websites-functionalitiesfeatures/ ] Link to comment Share on other sites More sharing options...
kongondo Posted February 22, 2018 Share Posted February 22, 2018 (edited) I haven't thoroughly tested this (nor fully read the post you linked to) but it works. Edit/Note: code be will be updated in line with the changes in OP. See new post below In /site/ready.php $pages->addHookAfter('save', function($event) { // get the page object saved $page = $event->argumentsByName('page'); // only on pages with a temporary parent (old template) if($page->template !='old-template') { $this->message('page has wrong template'); return; } // if page still unpublished return if($page->is(Page::statusUnpublished)) { $this->message('page still unpublished'); return; } // good to go; assign new parent and template $page->parent = '/new-parent/'; $page->template = 'new-template'; $page->save(); $this->message('page template and parent changed'); }); Messages in there are for testing. Edit as needed Edited February 25, 2018 by kongondo 3 Link to comment Share on other sites More sharing options...
Christophe Posted February 22, 2018 Author Share Posted February 22, 2018 (edited) Thanks @kongondo! Edit: Not sure this is what I need... Edited February 24, 2018 by Christophe Removed some things Link to comment Share on other sites More sharing options...
kongondo Posted February 25, 2018 Share Posted February 25, 2018 Just created a new post. This takes into account your updated OP. Since you are in a multi-lingual setup, I've decided to use IDs rather than names. Less user-friendly, but with inline comments, you should be fine. In /site/ready.php $pages->addHookAfter('save', function($event) { // get the page object saved $page = $event->argumentsByName('page'); // only on pages with the temporary template or their parent has the temporary template if($page->template !='directory_page_temporary' || $page->parent->template != 'directory_listing_temporary') { $this->message('page has wrong template'); return; } // if page still unpublished return if($page->is(Page::statusUnpublished)) { $this->message('page still unpublished'); return; } // no choix made yet, return. qui_1 is a page field of single type if (!$page->qui_1){ $this->error('You need to make a choix!');// OR show message instead //$this->message('You need to make a choix!'); return; } // good to go; assign new parent and template # determine new parent and new template $p = ''; $t = ''; // choix: mannequin-modele (this choix page has ID 1024; easier to check by ID in multi-lingual site) if($page->qui_1->id == 1024) { $p = 1021;// new parent is mannequins-modeles and has ID 1021 $t = 'directory_page_mm'; } // choix: autre-artiste (this choix page has ID 1025; easier to check by ID in multi-lingual site) elseif($page->qui_1->id == 1025) { $p = 1022;// new parent is autres-artistes and has ID 1022 $t = 'directory_page_autre_artiste'; } # save if($p && $t) { $page->parent = $p; $page->template = $t; $page->save(); $this->message('page template and parent changed'); } else { // some error } }); 2 Link to comment Share on other sites More sharing options...
Christophe Posted February 26, 2018 Author Share Posted February 26, 2018 (edited) Hello, And thank you for the code for site/ready.php. WIth comments,etc. I think I understand its logic, at least enough for the moment. I've started looking at the Hooks pages/docs again. The code doesn't seem to work currently. I can't know why. No error messages. I've modified the id's. Yes, in case the website has another language activated, it's better to use id's :). And for qui_1->id, the id is the id of the page being referenced as a choice, so I changed it well normally. The field is mandatory. I've filled in and sent 2 fake form instances to test ready.php. When I click on publish for these pages nothing seems to happen. The page stays under the temporary parent, with the temporary template. I see "Session: Page saved: /.../ (1 change/modification). And also, I see "Session: Modification : status" and "Session: page still unpublished" but only after publishing then unpublishing the page(s). I have debug mode (still) on. Perhaps $page->save(); should be used twice? As the new template is only available when the new parent is "saved". But perhaps it's different with the api(?). It could be anything that makes it not working of course :/. (If it's needed, I can provide more information, etc.) Have a nice day/week! NB: perhaps the order of some things/"commands" have to be changed(?). Or perhaps in finished.php (But, "Admittedly, this is probably not the place you would put hooks,..."). Edit: should I install Tracy Debugger? Edited February 26, 2018 by Christophe Link to comment Share on other sites More sharing options...
kongondo Posted February 26, 2018 Share Posted February 26, 2018 12 minutes ago, Christophe said: The code doesn't seem to work currently. I can't know why. No error messages. OK, let's try debug this. The code works for me, so maybe I didn't understand your setup. 13 minutes ago, Christophe said: And for qui_1->id, the id is the id of the page being referenced as a choice, That's what I have in my code. There are two possible pages that can be referenced. (1). mannequin-modele with ID 1024 OR (2) autre-artiste with ID 1025. Of course, these are the IDs I used in my example and need to be edited as per your needs. I'll try to post an animated GIF/video of my test setup to see if it matches yours. 16 minutes ago, Christophe said: I've filled in and sent 2 fake form instances to test ready.php. This bit I don't get. First, I thought the child pages are edited manually in the backend. A choice is made, the page is published which triggers the Hook to move them to their new parent and change their template. Is that not the case? What form is this? What sort of code does it contain? Is it being sent from the frontend? Secondly, are you saying you are posting the form to ready.php? How are you doing that? I am not sure ProcessWire will allow you to do that. Incidentally, are we talking about the same ready.php found (or created) under /site/ready.php ? 22 minutes ago, Christophe said: Edit: should I install Tracy Debugger? Without question, yes . I'm happy to have a look if the site is online if I could get a temporary access. 2 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