Events Overview

Elgg has an event system that can be used to replace or add to core functionality. When certain things happen, these events are triggered and handlers than have been registered for them are called. There are two types of events that you can register handlers for: Elgg events and plugin hooks.

Elgg Events

Elgg events are triggered when something is created, updated or deleted or when the Elgg framework is loading. Arbitrary events can also be triggered by any plugin. Each event is determined by an event name and an object type (system, user, object, relationship name, annotation, group).

For example, whenever a new blog is created, the following is triggered where `$blog` is the newly created entity:

elgg_trigger_event('create', 'object', $blog);

This calls all functions which have been registered for the 'create', 'object' event. To register for an event, you define a function that receives the event parameters and returns either true or false, then you call the elgg event registration function, specifying the event you want to listen for. For example:

//Display a system message whenever an object is created
function foobar($event, $type, $object) {
    system_message("Foobar event handler has been triggered!");
}
 
elgg_register_event_handler('create', 'object', 'foobar');

A function that returns false prevents any further event handlers from being called. It may also stop code in the core for running; for example it may prevent an entity from being deleted.

See the event reference for details on how to construct your event handler functions and register for a particular event.

Plugin hooks

Plugin hooks are similar to Elgg events in many ways. You can register handlers to be run when a hook is triggered just like you can with events. However, instead of being triggered in response to something "happening" in the system, they are generally triggered with the purpose of allowing plugins to customize some data which will change the behavior of the code that follows. Plugin hooks will frequently return non-boolean values, and subsequent handlers will continue to execute even when a value of false is returned.

For example, input validation works via a plugin hook. The htmLawed plugin registers for this hook, sanitizes the data passed to it, and returns the sanitized data.

Whenever configuration options are involved it's a good idea to introduce a plugin hook, since that allows any other plugin to manipulate the options without overwriting or rewriting the code.

See the plugin hooks reference for details on how to construct your plugin hook handler functions and register for a particular hook.

What's the difference?

Don't fret if you're confused about the differences between events and plugin hooks -- it's a subtle distinction. The short answer is that plugin hooks are more flexible than elgg events. For more detail, see the documentation on plugin hooks.

Search docs