Using ZOO events

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

Using the ZOO events system

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

Events are a neat concept to listen for actions in the 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 Joomlas plugin system. There are quite a few events predefined in ZOO, have a look at ZOOs config.php to checkout 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 the ZOO. As a second step, you retrieve the 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 the 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'].

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. The ZOOevent plugin is a Joomla system plugin, that loads on every pageload. You can surely refine that part a bit, to suit your needs. That's it, you are good to go.

Documentation on Github

Help us out! If you are feeling that our documentation has errors or can be improved, fork it at Github and send us a pull request. Any contribution is much appreciated. Thank you!