README

NOTE: This is obsolete in Contao 4 - you should use the event dispatcher from the symfony kernel directly for
Contao 4 only extensions.
See below for maintaining compatibility with both Contao versions.

Event dispatcher for Contao Open Source CMS

Why an event dispatcher for Contao Open Source CMS, are the hooks not enough?
First you need to understand, there is no real difference between hooks and events.
The are both notifications from within the system.

But events are more elastic than hooks. They can be hand round, consumed, stopped or bubble upon a hierarchy.

The event dispatcher can handle every form of callbacks, like closures or static methods.

Listen on events

The event dispatcher provide two ways to listen on events.

First and mostly used is an event listener. It is designed to listen on a single event.

Second the event subscriber is designed to listen on multiple events.

Event listener per configuration

Since version 1.3 there are two ways to define your listeners per configuration.

/config/event_listeners.php

We recommend to use this method!

The file /config/event_listeners.php must return an array of event names as keys and listeners as values.

<?phpreturnarray(// With a closure'event-name'=>array(function($event) {// event code } ),// With a static callable'event-name'=>array(array('MyEventListener', 'myCallable') ),// With an object callable'event-name'=>array(array(newMyEventListener(), 'myCallable') ),// With a service object'event-name'=>array(array($GLOBALS['container']['my_event_listener'], 'myCallable') ),// You can wrap the listener into an array with a priority'event-name'=>array(array($listener, $priority) ),);

/config/config.php

In your /config/config.php use $GLOBALS['TL_EVENTS'] to register your event handlers.

Handle with priority

To define the priority, you can use an array with the listener as first and the priority as second element.

$GLOBALS['TL_EVENTS']['event-name'][] =array($listener, $priority);

Event listener per code

$container['event-dispatcher']->addListener('event-name', $listener);

Event subscriber per configuration

Since version 1.3 there are two ways to define your listeners per configuration.

/config/event_subscribers.php

We recommend to use this method!

The file /config/event_subscribers.php must return an array of subscribers.

<?phpreturnarray(// With a factoryfunction($eventDispatcher) {returnnewMyEventSubscriber(); },// With an object class name'MyEventSubscriber',// With an object instancenewMyEventSubscriber(),// With a service object$GLOBALS['container']['my_event_subscriber'],);

/config/config.php

In your /config/config.php use $GLOBALS['TL_EVENT_SUBSCRIBERS'] to register your subscribers.