System events (triggers) of ZOO framework

9.5/10 rating (2 votes)

Almost any framework has such concept as system events (triggers, hooks, traps). In joomla they are implemented as plugins. And in the classical literature they are usually called Observers or listeners.

Here is what they do. Event triggers are placed in various places in the code. For example, item:deleted event will be triggered after any item is deleted. All functions that are connected to this event will be executed in order of their connection. Thus, things related to an item will be deleted as well, such as images or database records.

How does it work?

Everything is made pretty easy within ZOO (actually, like in Joomla itself) and is gathered in a single event helper.

Creating and connecting an event class.

$event         = $this->app->event;
$dispatcher = $event->dispatcher;
// registering our class
// connecting to the event
$dispatcher->connect('myevent:function', array('MyEventClass', 'handler'));

If you want to create your own event then it’s all the same, just don’t forget to call dispatcher notification.

$dispatcher->notify($event->create($var, 'myevent:function', array( /* assoc array with additional parameters */))); // $var - main object of the event

An example of a simplest class for event processing

// A class should be connected manually
class MyEventClass
     * Simple event handler
     * @param AppEvent $event
    public static function handler($event)
          $app = App::getInstance('zoo'); // do not forget that this is a static method without $this
          $subject = $event->getSubject(); // getting an event object
          $params = $event->getParameters(); // getting additional parameters

Existing system events

JBZoo registers empty classes to simplify functionality enhancement. They can be found here - /media/zoo/applications/jbuniversal/framework/events/jbevent.*.php . This way you can also find out what kind of events a system has.

It is important to understand that:

  • Event handlers are only static classes.
  • All objects are passed using a link so their changes will affect the rest of the code.
  • It does not matter what you will return from an event - it does not used anywhere.
  • You can’t break the actions chain inside an event.
  • It is very easy to create a loop. For example, by calling the creation of a similar instance on init.

Still gotta questions, who can help me?

Visit our special JBZoo tech support forum which is specifically meant for JBZoo so the priority of the response and its promptness are much better there than in any other place. With inactive account with a “Plus” sign in a tariff plan you can ask for support in one of these sections or use any of our contacts.