Juergen Posted November 8, 2017 Share Posted November 8, 2017 Hello @ all, I have the following scenario: A parent page status should be changed depending on the number of children. If there is at least 1 child the status should be published, if there are no children the statuts should be unpublished. Seems easy to realize at first sight, but ... the button of the parent page should also change. If there are children (at least 1): Only the save button should be displayed. If there are no children: The 2 buttons should be displayed. The children will be created or deleted via a Pagetable field and can be also created by clicking a checkbox in the parent page. I have tried to hook into "added" and "deleted" and also "trashed" to set the page->parent->status and it seems to work, but the button appearence will not change. So the status of the parent page seems to be changed but not the buttons (that is the problem) . Here is what it looks like after deleting of the last child: As you can see the status of the page was changed to "2048" which is unpublished, but the button is the same as before (no change). If you take a look into the settings tab you will see that the checkbox for unpublish is also not checked: The buttons change only if I hook into "before save ready" but this kind of hook doesnt take care of added or delete pages. It is also not possible to check for hasChildren because the creation of the children take place after saving of the parent page. Does someone has struggled with the same problem and has a working solution? Another approach would be to prevent the deletion of the last child. Link to comment Share on other sites More sharing options...
Juergen Posted November 8, 2017 Author Share Posted November 8, 2017 An idea would be to check the number of children before the deletion. If there is only 1 child than prevent processing of the delete function. Maybe lets say a hook that prevents the execution of the deletion???? Something like $event->replace = true; Link to comment Share on other sites More sharing options...
Juergen Posted November 8, 2017 Author Share Posted November 8, 2017 Ok, I have found a solution to prevent the deletion of a page: $wire->addHookBefore("Pages::trash", function($event) { $page = $event->object; $event->replace = true; // now original function won't be called $event->return = $this->message("Trashing not allowed!"); }); So it must be adapted to only run on certain templates and if there is only 1 child. Link to comment Share on other sites More sharing options...
Juergen Posted November 8, 2017 Author Share Posted November 8, 2017 Here is the final code the prevents the deletion of the last child: I restrict it that the child is type of one of the following templates: single-date, single-event, single-business-vacation or single-special-business-hours. //Prevent deletion of the the last child $wire->addHookBefore("Pages::trash", function($event) { $page = $event->arguments(0); if(!in_array($page->template->name, array('single-date', 'single-event', 'single-business-vacation', 'single-special-business-hours'))) return; $parent = $page->parent; $childrennumber = count($page->parent->children); if($childrennumber === 1) { $event->replace = true; // now original function won't be called $event->return = $this->message("Trashing not allowed! There must be at least 1 page."); } }); I put a complete description in the Tutorial section 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