Back to EventAware class

Method triggerEvent

public array
triggerEvent
(mixed $eventName, mixed $args = [])
Calls all handlers associated with an event group.
Parameters
  • string $eventName The event name.
  • array|\Joomla\Event\Event $args An array of arguments or an Event object (optional).
Returns
  • array An array of results from each function call. Note this will be an empty array if no dispatcher is set.
Since
  • 4.0.0
Deprecated
  • 5.0
-
  • \InvalidArgumentException
Class: EventAware
Project: Joomla

Method triggerEvent - Source code

/**
 * Calls all handlers associated with an event group.
 *
 * This is a legacy method, implementing old-style (Joomla! 3.x) plugin calls. It's best to go directly through the
 * Dispatcher and handle the returned EventInterface object instead of going through this method. This method is
 * deprecated and will be removed in Joomla! 5.x.
 *
 * This method will only return the 'result' argument of the event
 *
 * @param   string       $eventName  The event name.
 * @param   array|Event  $args       An array of arguments or an Event object (optional).
 *
 * @return  array  An array of results from each function call. Note this will be an empty array if no dispatcher is set.
 *
 * @since       4.0.0
 * @throws      \InvalidArgumentException
 * @deprecated  5.0
 */
public function triggerEvent($eventName, $args = [])
{
    try {
        $dispatcher = $this->getDispatcher();
    } catch (\UnexpectedValueException $exception) {
        $this->getLogger()->error(sprintf('Dispatcher not set in %s, cannot trigger events.', \get_class($this)));
        return [];
    }
    if ($args instanceof Event) {
        $event = $args;
    } elseif (\is_array($args)) {
        $event = new Event($eventName, $args);
    } else {
        throw new \InvalidArgumentException('The arguments must either be an event or an array');
    }
    $result = $dispatcher->dispatch($eventName, $event);
    // @todo - There are still test cases where the result isn't defined, temporarily leave the isset check in place
    return !isset($result['result']) || \is_null($result['result']) ? [] : $result['result'];
}