Juergen Posted November 8, 2017 Share Posted November 8, 2017 Hello, this is an issue I was struggeling for a while to find a working solution: The goal: A page must contain at least 1 child page. If there are more child pages than prevent the editor (user) from deleting the last child page and output a message that the deletion of the last page is not allowed. Idea behind this: I have pages which holds all the information of events, but the date or dates of the event are all located in child pages: - event page - event date 1 - eventdate 2... and so on. Each event must have at least on date. An event with no date doesnt make sense. This was the reason to not allow the deletion of the last date. Here is the code that you should copy into your ready.php $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->warning("Trashing not allowed!"); } else { $this->message("1 Page was deleted"); } }); 1) You must hook before the page will put to trash so "addHookBefore" has to be used 2) If you want to restrict it to certain types of templates than put the names of the templates in an array (in this case the templates I want to run the hook are "single-date, single-event, single-business-vacation or single-special-business-hour". If you want it to run on all pages simply remove this line from the code: if(!in_array($page->template->name, array('single-date', 'single-event', 'single-business-vacation', 'single-special-business-hours'))) return; 3) You need to get the number of all child pages from the parent to check if it is the last child or not. If there are more than 1 page and you delete on of them you get the message "1 page deleted" - so everthing is ok and the deletion of the page take place 4) If it is the last child than the trash function will be replaced by a message. So the trash function will not be executed and the warning message will be displayed instead. 5) Inform the editor with this message that the deletion of the last child is not allowed. I use here the message type "warning" (color orange), but you can also use "error" or "message" Thats all - hope this is useful for others that need something similar Only to mention: This works also via Ajax deletion fe in the page tree, but in first instance it seems that the last child was deleted because it disappears, but after refreshing the page the last child will be visible again. 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