Jump to content

Is it possible to guess has a hook been triggered by admin page or not?


theoretic
 Share

Recommended Posts

Hi friends! And thanks for Processwire!

I have an interesting question. Couldn't find an answer myself.

Let's suppose we have a hook of this kind:

$wire->addHookAfter("Pages::saveReady", function($event) {
	$user = $event->arguments(0);
	//some manipulations with user data
}

This hook can be triggered both by frontend (there are some forms there allowing users to edit their profiles) and by backend (when superuser saves user page).
I'd like this hook to have different behavior in these two cases. My first guess was like this:

$wire->addHookAfter("Pages::saveReady", function($event) {
	$user = $event->arguments(0);
	$page = event->object;
	if( $page->template == 'admin' ):
		//some admin-specific manipulations with user data
}

No way! $page is always null, no matter how the hook was triggered.
Any idea how to guess which page has triggered the hook?
Thanks in advance for possible help ?
 

Link to comment
Share on other sites

38 minutes ago, theoretic said:

This hook can be triggered both by frontend (there are some forms there allowing users to edit their profiles)

There are many possible solutions and it depends how you implemented these forms and how you submit the data so it would be good to share some more details here ? 

Link to comment
Share on other sites

So I guess you are calling $page->save() somewhere in that endpoint, right? In that case you can just add whatever runtime property you want to that page and then use that as a flag in your hook:

<?php
// api endpoint
...
$page->isFrontendSave = true;
$page->save();

// hook, eg in init.php
$wire->addHookAfter("Pages::saveReady", function($event) {
  $page = $event->arguments(0);
  if($page->isFrontendSave) {
    // do something
  } else {
    // do something else
  }
});

 

  • Like 3
Link to comment
Share on other sites

2 hours ago, theoretic said:
$wire->addHookAfter("Pages::saveReady", function($event) {
	$user = $event->arguments(0);
	$page = event->object;
	if( $page->template == 'admin' ):
		//some admin-specific manipulations with user data
}

In your Pages::saveReady hook, the $event->object is actually \Processwire\Pages.

The actual page being saved is what's returned from $event->arguments(0).  In this case you are assigning it to $user, but that won't always be meaningful; only when you're saving a user.

Bernhard's solution will work of course, but it may also be advisable to assign $event->arguments(0) to the variable $page (rather than $user) to avoid confusion in the future. 

You can then check for $page-template == 'user' if you want to do something specific for users being saved.

  • Like 2
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...