
As already mentioned, there are two types of events in the Elgg platform: Elgg events and plugin hooks. Elgg events are triggered when something is created, updated or deleted or when the Elgg framework is loading. Examples would include a blog post being created or a user logging in. Each event is determined by an event name and an object type (system, user, object, relationship name, annotation, group). By registering handlers for Elgg events, your plugin can have code executed when that event occurs.
Contents |
To register a handler, use the function register_elgg_event_handler as in this example:
register_elgg_event_handler('init','system','entities_init');
v1.8
elgg_register_event_handler('init','system','entities_init');
object here does not refer to an ElggObject but rather any object in the framework: system, user, object, relationship name, annotation, group.
Your event handler should have the following prototype:
function event_handler_function($event, $object_type, $object) { ... do stuff ... }
If the function returns false, it halts the calling of other event handlers and may stop the execution of some code in the Elgg core.
You can trigger an event in your plugin with the trigger_elgg_event function.
trigger_elgg_event($event, $object_type, $object = null)
Example: trigger_elgg_event('validate', 'user', $user);
Elgg has four events that occur on almost every pageload.
System boot connects to the database, starts the session, and establishes some base variables and configuration settings. This event is inaccessible to plugins, as it is triggered before plugins are loaded.
This event is intended for plugins to perform configuration procedures. It is recommended that you add submenu items and perform similar activities in a yourplugin_init function attached to this event.
Sometimes it's necessary to perform further procedures after the full Elgg framework has been loaded, but before any HTML content has been created. This event is called during the first reference to elgg_view (whether directly or through a call to a helper function like elgg_view_title or list_entities).
This event is triggered after the page has been fully rendered and sent to the browser. Registering to this event can be useful to perform events which may take a bit of time without slowing the user experience.