Jump to content

hook property: calling with argument(s)?


theoretic
 Share

Recommended Posts

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

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...