LAPS Posted January 18, 2018 Share Posted January 18, 2018 Hi, I am a newbie in PHP, and I am learning PW including hooks. In my ready.php file I have PHP code and a PW hook stated as follow: $object = ...; wire()->addHookBefore('Pages::save', function ($event) use (&$object) { ... } I would like to define a 'hookFunction' separately, something like this: wire()->addHookBefore('Pages::save', null, 'hookFunction'); However the issue is in stating separately the 'use (&$object)' part. I know that the 'use' language construct is used for "anonymous functions" and the '&' is used for "passing by reference", but how can I state the hook function separately? P.S.: Maybe there is a better way (that I do not know) to state the above, hook-related code at all. Link to comment Share on other sites More sharing options...
kongondo Posted January 18, 2018 Share Posted January 18, 2018 (edited) 30 minutes ago, LAPS said: I would like to define a 'hookFunction' separately, something like this: wire()->addHookBefore('Pages::save', null, 'hookFunction'); Did this not work? It should. wire()->addHookBefore('Pages::save', null, 'hookFunction');// grab function hookFunction(HookEvent $event) { // do your stuff } http://processwire.com/api/hooks/ Edited January 18, 2018 by kongondo Link to comment Share on other sites More sharing options...
LAPS Posted January 18, 2018 Author Share Posted January 18, 2018 @kongondo, I tried many ways to state that particular hook separately, including your that does not work. It seems that, even if the 'hookFunction' is called, within the 'hookFunction' the $object is not modified "persistently", maybe because it is not passed by reference. This way, the $object value modified internally to the 'hookFunction' is not modified for use within other, subsequent hooks-functions. Link to comment Share on other sites More sharing options...
Robin S Posted January 19, 2018 Share Posted January 19, 2018 Not entirely sure about the following, so would be glad to hear from anyone who understands this better. @ryan? I think you must pass in any external variables to a named hook function as part of the $options array. See this line in the HookEvent class: @property array $options An optional array of user-specified data that gets sent to the hooked function. The hook handling method may access it from $event->data. Also includes all the default hook properties. So you would assign a custom key/value to the $options array by reference. An example: $colour = 'red'; $options = array(); $options['colour'] = &$colour; // assign by reference $wire->addHookAfter('Pages::save', null, 'hookFunction', $options); function hookFunction(HookEvent $event) { $opts = $event->options; $opts['colour'] = 'green'; // $colour is now 'green' // ... } 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