Actions

API16

JDispatcher/trigger

From Joomla! Documentation

< API16:JDispatcher
Revision as of 17:47, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Triggers an event by dispatching arguments to all observers that handle the event and returning their return values.

[Edit Descripton]

Description:JDispatcher/trigger

Syntax

trigger($event, $args=array())
Parameter Name Default Value Description
$event $event The event to trigger.
$args array() $args An array of arguments.

Returns

array An array of results from each function call.

Defined in

libraries/joomla/event/dispatcher.php

Importing

jimport( 'joomla.event.dispatcher' );

Source Body

public function trigger($event, $args = array())
{
        // Initialise variables.
        $result = array();
 
        /*
         * If no arguments were passed, we still need to pass an empty array to
         * the call_user_func_array function.
         */
        if (!is_array($args)) {
                $args = (array)$args;
        }
 
        $event = strtolower($event);
 
        // Check if any plugins are attached to the event.
        if (!isset($this->_methods[$event]) || empty($this->_methods[$event])) {
                // No Plugins Associated To Event!
                return $result;
        }
 
        // Loop through all plugins having a method matching our event
        foreach ($this->_methods[$event] AS $key)
        {
                // Check if the plugin is present.
                if (!isset($this->_observers[$key])) {
                        continue;
                }
 
                // Fire the event for an object based observer.
                if (is_object($this->_observers[$key])) {
                        $args['event'] = $event;
                        $result[] = $this->_observers[$key]->update($args);
                }
                // Fire the event for a function based observer.
                elseif (is_array($this->_observers[$key])) {
                        $result[] = call_user_func_array($this->_observers[$key]['handler'], $args);
                }
        }
 
        return $result;
}

[Edit See Also] SeeAlso:JDispatcher/trigger

Examples

<CodeExamplesForm />