Jump to content

how do you pass variables between modules?


benbyf
 Share

Recommended Posts

How would one pass a variable set in one module to another module via a Hook?

e.g.

module 1.

public function ___saveUser($vars){
        $userSaved = true
}

module 2

public function init() {
	$this->addHookAfter('module1::saveUser', $this, 'userSaved');
}
public function userSaved($e){
	$userSaved = $e->userSaved;
	return $userSaved; // true?
}

???

Link to comment
Share on other sites

module 1

// define a property
protected $userSaved = null;

public function ___saveUser($vars){
    // add to the class property
    $this->userSaved = true;
}

// magic get allows to access private and protected properties too, not only public properties
public function __get($what) {
    if(isset($this->$what)) {
        return $this->$what;
    }
    // or a more restricted and controlled method:
    $validProperties = array('userSaved', 'someOther');
    if(in_array($what, $validProperties) {
        return $this->$what;
    }
}

module 2

public function init() {
	$this->addHookAfter('module1::saveUser', $this, 'userSaved');
}

public function userSaved($event){
    $userSaved = $event->userSaved;
    return $userSaved;

    // or if you want to change the return value of the originating event, you need to change its $event->return!
    $changedBooleanValue = true; // do something to calculate the result
	$event->return = $changedBooleanValue;  // pass it back to the hooked event

    // if you want to access the value that was passed into the hooked method, you get it with
    $vars = $event->arguments[0];  // there is also a method like: argumentByName or that like, please refer to the docs or code base for it
    ...
}

 

  • Like 4
Link to comment
Share on other sites

No, accessing a variable set in the scope of the hooked method only cannot be accessed. You need to pull it into the classes scope, like I shown in the module 1 example.

You may access the passed vars by $event->arguments[n],

and if your hooked function has a return value, you can get it and set it like:

    $resultOfhookedFunction = $event->return;

    // do some calculation, ...

    // pass back another result:
    $event->return = $calculatedResult;

 

  • Like 1
Link to comment
Share on other sites

Think I fundamentally don't really understand how hooks operate.

I now hav this in module one:

private $userName = "";

public function ___register($input){
	$this->userName = $input->post->name;
}

and module 2:

public function init() {
		$this->addHookAfter('Subscribers::register', $this, 'userSaved');
}
public function userSaved($e){
        $name = $e->userName;
        $log = wire('log');
        $log->message($name);
}

but still cant get anything printing out. and its extrememly hard to test this in working as teh hook happens andI have to catch it somewhere but I cant do var_dump etc when it all happens in teh background... :(

Link to comment
Share on other sites

11 minutes ago, benbyf said:

but I cant do var_dump etc when it all happens in teh background

TracyDebugger - use the Dumps Recorder panel and if you have to, use the "Preserve Dumps" checkbox (although you probably won't need it).

Then insert a bd($var) call into your code - easy as pie :)

PS Who uses var_dump anymore ;)

  • Like 2
Link to comment
Share on other sites

You have set

private $userName

You can set it to public, but only for fast testing, but better: use the magic get method as I showed you in my first post above.

You cannot access a private property from the outsite. The function that receives the Event is "the outside". :)

  • Like 2
Link to comment
Share on other sites

1 hour ago, benbyf said:

adding the magic __get() gives me lots of errors in the rest of my code to do with variable becoming null objects.I can no longer use $this->user for example within a function.

Hhm, don't have time to check this. But if you are not comfortable with the magic get ATM, you may also add a separate method for every property you need public read access to:

    public function getUserSaved() {
        return $this->userSaved;
    }

    public function getUserEmail() {
        return $this->userEmail;
    }

...

From within your hook function you may call this f.e. with

    $userSaved = $event->object->getUserSaved();

 

  • 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

×
×
  • Create New...