Jump to content

Update 2.6.7: Hooks on ready.php, init.php and finished.php also possible for backend hooks?


Juergen
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :-(

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...