Events

This short tutorial will show you how to use ZOO events system.


Usage

Events are an easy way for third party developers to hook into ZOO system. Right now we distribute a ZOOevents plugin with ZOO, that actually shows you how to register for those events.

Events are a neat concept to listen for actions in ZOO and react to those actions. An action would be the rendering of an item e.g. You could listen to that event and eventually modify the output. This is much like Joomla's plugin system. There are quite a few events predefined in ZOO, have a look at ZOOs config.php to check out all the events that are already defined. Also, checkout the events folder in ZOOs administration folder. There are examples of every event and its parameters.

Basically the workflow would be like this. You include ZOOs config.php to load ZOO. As a second step, you retrieve ZOO app. Then you can connect to ZOOs event dispatcher with any PHP callback.

// load ZOO config
require_once(JPATH_ADMINISTRATOR.'/components/com_zoo/config.php');

// Get ZOO App instance
$zoo = App::getInstance('zoo');

// register event
$zoo->event->dispatcher->connect('item:saved', array('MYCLASS', 'itemSaved'));

In this example we register to the item:saved event. As a parameter we pass the callback MYCLASS::itemSaved. That callback is being triggered whenever an item is saved.

class MYCLASS {

    public function itemSaved($event) {

        $item = $event->getSubject();
        $new = $event['new'];

        // do whatever you'd like to do

    }

}

This is how our callback would look like. The item that was just saved can be retrieved with the events getSubject function. Further you can determine if the item is new or has been edited with checking $event['new'].

Note The element:beforedisplay event is a somewhat special case, because you can prevent the element from being rendered, by setting $event['render'] to false.

You can react to any event. You just need to make sure, that you register to that event, before it is triggered. ZOOevent plugin is a Joomla system plugin, that loads on every page load. You can surely refine that part a bit, to suit your needs. That's it, you are good to go.

ZOO Documentation