Events

clearFusionCMS has an event system which allows modules to listen for an event and then when it arrives perform some operation. The event system allows modules to communicate between each other with no knowledge of each other beyond how to issue an event. This allows modules to function in a reduced state or with different functionality, e.g. Campaign Monitor or MailChimp modules may be installed to handle newsletter subscriptions, if either is present then the shopping cart module can make use of either but the cart has no knowledge of newsletters it simply raises the appropriate event. This also means that existing code can work with as yet unknown services with no change.

Raising Events

Events can be raised from within snippets and other code using:

clearFusionCMS::getEventManager()->notify($evt);

Where $evt is an instance of flEvent.

Handling Events

A module can choose the events to listen for during installation by using:

clearFusionCMS::getEventManager()->addListener('newsletter.subscribe', 'exampleEvents', 'onSubscribe');

Where the 1st parameter is the event to listen for, the 2nd is the name of the class that will handle the event and the final parameter is the name of the function to invoke on the class.

An example event handler class would be as follows, where when a newsletter.subscribe event occurs the subscriber is added to a list at the tolraMail service.

class exampleEvents {
	/**
	 * Event handler for adding subscribers.
	 *
	 * @param flEvent $evt The event object.
	 */
	function onSubscribe($evt) {
		$cm = new tolramail();
		$cm->subscribe(
			$evt->getParam('listId'),
			$evt->getParam('email'),
			$evt->getParam('firstName') . ' ' . $evt->getParam('lastName'),
			array()
		);
	}
}



TOP