I'd love to see:
- A simplified way to call events
- Plugins having more control over the input and output of methods with events
To that end I was thinking we could add the following method to the AppModel class:
/**
* Executes the given event
*
* @param string $event_name The name of the event to trigger
* @param array $params The list of parameters to submit to the event handlers
* @return array The list of parameters that were submitted along with any modifications made to them
* by the event handlers. In addition a __return__ item is included with the return array.
*/
protected function executeAndParseEvent($event_name, $params) {
$eventFactory = $this->getFromContainer('util.events');
$eventListener = $eventFactory->listener();
$eventListener->register($event_name);
$event = $eventListener->trigger(
$eventFactory->event($event_name, $params)
);
$return = ['__return__' => $event->getReturnValue()];
$event_params = $event->getParams();
if (is_array($event_params)) {
foreach ($event_params as $event_param => $data) {
if (array_key_exists($event_param, $params)) {
$return[$event_param] = $data;
}
}
}
return $return;
}
Then to trigger an event and allow it to update the submitted data, an event could be called as such:
extract($this->executeAndParseEvent('Packages.add', compact(['package_id', 'vars'])));
The call to extract() would override the submitted variables with the updated version (or the same thing if no updates were made).
I'd love to see:
To that end I was thinking we could add the following method to the AppModel class:
Then to trigger an event and allow it to update the submitted data, an event could be called as such:
extract($this->executeAndParseEvent('Packages.add', compact(['package_id', 'vars'])));
The call to extract() would override the submitted variables with the updated version (or the same thing if no updates were made).