Juergen Posted February 16, 2018 Posted February 16, 2018 Hello @ all, In my case I have some hooks with the same code which run on page "Pages::trash" and "Pages::delete" inside may ready.php. For the moment I have used separate hooks for each of them. Is there a possibility to combine them so the same code runs only once and includes "trash" and "delete"? Explanation: Instead of $wire->addHookBefore("Pages::trash", function($event) { .... run code }); and $wire->addHookBefore("Pages::delete", function($event) { .... run code }); combine them $wire->addHookBefore("Pages::trash","Pages::delete", function($event) { .... run code }); This would be more efficiently for further changes and reduces the code. Best regards
kixe Posted February 16, 2018 Posted February 16, 2018 // inside a class e.g. modules $this->addHookAfter('Pages::trash', $this, 'myHookMethodName'); $this->addHookAfter('Pages::delete', $this, 'myHookMethodName'); public function myHookFunction($event) { // ... } // outside a class e.g. inside ready.php or templates wire()->addHookAfter('Pages::trash', null, 'myHookFunctionName'); wire()->addHookAfter('Pages::delete', null, 'myHookFunctionName'); function myHookFunction($event) { // ... } 5
Zeka Posted February 16, 2018 Posted February 16, 2018 Hi @Juergen I'm not sure about $wire->addHookBefore("Pages::trash","Pages::delete", function($event) { .... run code }); But you can define function separately wire()->addHookAfter('Class::method', null, 'myHookFunctionName'); 1
Juergen Posted February 16, 2018 Author Posted February 16, 2018 11 minutes ago, Zeka said: wire()->addHookAfter('Class::method', null, 'myHookFunctionName'); Thanks @Zeka, but I guess in this case I have to define the functions for each "Class::method" - so I have also 2 functions ("Pages::trash", "Pages::delete") - so I can leave the 2 hooks as they are.
Juergen Posted February 16, 2018 Author Posted February 16, 2018 27 minutes ago, kixe said: // outside a class e.g. inside ready.php or templates wire()->addHookAfter('Pages::trash', null, 'myHookFunctionName'); wire()->addHookAfter('Pages::delete', null, 'myHookFunctionName'); function myHookFunction($event) { // ... } Aaahh, I see!! This would be a solution Thanks!!! 1
Juergen Posted February 16, 2018 Author Posted February 16, 2018 Here is my code for others who also deals with the same issue. This little function prevents the deletion of the last child page if page has a certain template. Code runs inside ready.php. //prevent deletion of last child function wire()->addHookBefore('Pages::trash', null, 'preventdeletionlastevent'); wire()->addHookBefore('Pages::delete', null, 'preventdeletionlastevent'); function preventdeletionlastevent($event) { $page = $event->arguments(0); if(!in_array($page->template->name, ['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 = wire()->warning(__("Deleting of the last date is not allowed. There must be at least 1 date.")); } else { wire()->message(__("1 date was deleted.")); } } 2
szabesz Posted February 16, 2018 Posted February 16, 2018 1 hour ago, Juergen said: $childrennumber === 1 How about <= 1 ? One never knows... 2
dragan Posted February 16, 2018 Posted February 16, 2018 ahem... don't you want to use camelCase? preventDeletionLastEvent would be more readable than preventdeletionlastevent -> https://processwire.com/api/coding-style-guide/#4.6-methods 4
Soma Posted February 16, 2018 Posted February 16, 2018 I wonder why you need something like this in the first place "to have at least one date". wire()->addHookBefore('Pages::trash', null, 'hookTrashDelete'); wire()->addHookBefore('Pages::delete', null, 'hookTrashDelete'); function hookTrashDelete($event) { $page = $event->arguments("page"); //prevent deletion of last child $preventTpls = [ 'single-date', 'single-event', 'single-business-vacation', 'single-special-business-hours' ]; if(in_array($page->template, $preventTpls)) { preventDeleteDateEntry($event, $page); } } function preventDeleteDateEntry($event, $page){ if($page->parent->numChildren() === 1) { $event->replace = true; // now original function won't be called wire()->warning(__("Deleting of the last date is not allowed. There must be at least 1 date.")); } else { wire()->message(__("1 date was deleted.")); } } I would code it like this. So the hook could be used for different things. Also some code formatting for better reading. Use $page->numChildren(), in case you have thousands/millions of children you'd get a server out of memory Don't use $event->return = error. 3
Jan Romero Posted February 16, 2018 Posted February 16, 2018 I don’t know your site, but wouldn’t you want to count only the siblings whose templates are also in $preventTpls? 1
Juergen Posted February 16, 2018 Author Posted February 16, 2018 In my case I want to count the children which are under the current page beeing edited. Each child page is a date, so I count of how many dates this page has. But counting the children via siblings could also be an opportunity.
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