Juergen Posted July 5, 2015 Posted July 5, 2015 Hello @ all, I have tried the new hook files (init.php, ready.php and finished.php) from the latest update (2.6.7) on the dev version and they work well on frontend. But I havent got it to work in backend. Here is a test example that runs on frontend as expected: $page->addHookAfter('render', function($event) { $page = $event->object; //test if($page->template == 'pricelist') { // $value contains the full rendered markup of a $page $value = $event->return; $value = str_replace("</body>", "<p>Hello World!</p></body>", $value); // set the modified value back to the return value $this->message("Hello world added"); $event->return = $value; } //test end }); It is only a test that adds "Hello world" on pages with the template "pricelist" on the frontend. But it doesn add anything in the backend on the pricelist template. Is it possible to use this new files to add hooks in the backend too and if possible how? At the moment I use a module from Soma to add hooks in the backend (HookAfterPageSave-Module). Thank you for your answeres
Ivan Gretsky Posted July 5, 2015 Posted July 5, 2015 I think all pages in admin have template=admin. 1
LostKobrakai Posted July 5, 2015 Posted July 5, 2015 Also keep in mind, that even the page-editing page in the backend isn't "$page->template == 'pricelist'". It's a hidden page of the admin template with the process PageEdit and it just shows the specific page values / edit form. I think all pages in admin have template=admin. Not all of them. Users for example aren't, but you can only access the process pages. 1
Juergen Posted July 6, 2015 Author Posted July 6, 2015 if(($page->template == 'pricelist') OR (($page->template == 'admin'))) { The addition "$page->template == 'admin'" fetches the admin template in backend too, but in this case the hook is added to every template in the admin. I have tried to add the page id of the pricelist template in the admin section too but this doesnt work. if(($page->template == 'pricelist') OR (($page->template == 'admin') AND ($page->id == '1409'))) { Is there a way to fetch only a specific admin template?
Martijn Geerts Posted July 6, 2015 Posted July 6, 2015 probably: $page->process == 'NameOfTheProcess'
Juergen Posted July 6, 2015 Author Posted July 6, 2015 probably: $page->process == 'NameOfTheProcess' No this doesnt work. It is also not possible to choose one specific template with this condition, because more templates have the same process :-(
Martijn Geerts Posted July 6, 2015 Posted July 6, 2015 So far I understand only the admin template has a field called process and in most circumstances a process assigned to it. I'm not aware of a process that runs on multiple templates.
LostKobrakai Posted July 6, 2015 Posted July 6, 2015 It seems Juergen is not talking about the "actual" template of a page, but the template of an edited page. There's a function on ProcessPageEdit to get the page you're editing (ProcessPageEdit::getPage). You really need to understand the fundamental difference between the frontend and the backend. The frontend is rendering the pages themself, while the backend actually consists mostly of pages with linked process modules (the ones you see in the page-tree as children of "Admin"). So the page being rendered in the backend is always the process, not a potential page being edited. If a process is showing a specific page you would need to ask the module to tell you which page it is. ProcessWire won't know. It's kinda like if you would use urlSegments on the frontend. The $page objects is always the same, but depending on the segment you're rendering a different UI. ProcessPageEdit does work the same. The $page object is the process, but it's rendering the form depending on the passed page ID. 1
Juergen Posted July 6, 2015 Author Posted July 6, 2015 Its not a really big problem. Somas module work as expected. It would only be nice if all hooks from front and backend could be managed without a module in one place.
Ivan Gretsky Posted September 2, 2015 Posted September 2, 2015 I think you can write a hook that will work both in frontend and admin. But you have to get the $page like this: $page = $event->arguments[0]; It seems like $page = $event->object; returns the actual page, which is admin with ProcessPageEdit in admin. But the first value in arguments array is the page we want to get in both cases, at least for saveReady event. 1
Juergen Posted September 2, 2015 Author Posted September 2, 2015 Thank you Ivan for your contribution. I will give this a try.
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