Details
-
Type: Sub-task
-
Status: Closed
-
Priority: Major
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
Description
Let's create a method to:
- A simplified way to call events
- Plugins having more control over the input and output of methods with events
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 handler * @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 from the event. */ 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; }