theoretic Posted June 23, 2023 Share Posted June 23, 2023 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 More sharing options...
bernhard Posted June 23, 2023 Share Posted June 23, 2023 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 More sharing options...
theoretic Posted June 23, 2023 Author Share Posted June 23, 2023 Okay, thanks! Admin pages are handled in their normal way, have nothing specific to add here. As to the frontend, the form is ajax-submitted to an API endpoint written by myself. Link to comment Share on other sites More sharing options...
bernhard Posted June 23, 2023 Share Posted June 23, 2023 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 } }); 3 Link to comment Share on other sites More sharing options...
iank Posted June 23, 2023 Share Posted June 23, 2023 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. 2 Link to comment Share on other sites More sharing options...
theoretic Posted June 23, 2023 Author Share Posted June 23, 2023 @bernhard, thanks! Quite an elegant solution, works like a charm. 1 Link to comment Share on other sites More sharing options...
szabesz Posted June 24, 2023 Share Posted June 24, 2023 Also worth noting: 3 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