theoretic Posted April 20, 2022 Share Posted April 20, 2022 Hello everybody! And thanks for Processwire! I have an interesting task which i couldn't resolve in the way i'd like. Hope someone will give a good advice ? So let's suppose we have pages with template called 'event'. We need an easy way to check whether a user can participate in this or that event. In such cases i'd like to write a hook of this kind: //somewhere in site/ready.php $wire->addHookProperty('User::canParticipateIn', function($event) { //deciding whether user can participate or not $event->return = $result; }); //somewhere in a template file if( $user->canParticipateIn ){ //displaying a form etc. } This generally works with simpler tasks. But in this particular case we need to pass an argument to our newly created hook function. This argument will be an event page instance. So our possible hook should change to something like pseudocode below: //somewhere in site/ready.php $wire->addHookProperty('User::canParticipateIn', function($event) use ($page) { //deciding whether user can participate or not $event->return = $result; }); //somewhere in a template file if( $user->canParticipateIn($eventPage) ){ //displaying a form etc. } Unfortunately this doesn't work. Processwire gives an error: Quote Method User::canParticipateIn does not exist or is not callable in this context So is there any way to pass an argument to a hook in this case? Thanks in advance for any possible answer! Link to comment Share on other sites More sharing options...
Zeka Posted April 20, 2022 Share Posted April 20, 2022 Hi @theoretic You are adding a property via hook instead of method https://processwire.com/api/ref/wire/add-hook-method/ 1 Link to comment Share on other sites More sharing options...
Zeka Posted April 20, 2022 Share Posted April 20, 2022 https://processwire.com/docs/modules/hooks/#how-can-i-add-a-new-method-via-a-hook Link to comment Share on other sites More sharing options...
MarkE Posted April 20, 2022 Share Posted April 20, 2022 Personally I wouldn’t use a hook for this. I would use a custom page class called EventPage and put the method in that. See https://processwire.com/blog/posts/pw-3.0.152/#new-ability-to-specify-custom-page-classes 1 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