Elgg provides two types of events:
- elgg events
- plugin hooks
Elgg Events
Elgg events are triggered when Elgg is loaded or a content is created, modified or deleted. Events are triggered in response to an action by a user. Naturally, you would like your plugin to respond to relevant events triggers. To enable a plugin to repond to event triggers, event handlers must be registered. For example, the following code tells the system to call the function snazzy_init() when this plugin is initialized.
register_elgg_event_handler('init','system','snazzy_init');
The first parameter is the event type, which is initialization in this example. The second parameter is the type of object e.g. system, user, etc. The third parameter is the function which is to be called in response to the event trigger. The fourth parameter is priority of execution whose value has to be an integer.
Event type can be init, boot, all, pagesetup, log, update, create, friend, profileupdate, plugin_boot, delete, join, leave, or annotate.
- all: These events are always triggered e.g. to log actions.
- boot: These event handlers are called when elgg boots i.e. connect to database, start session, and set configuration variables. These event handlers are triggered before plugins are loaded so they are not accessible to plugins.
- init: init events are generally the first function called in a plugin. They are specific to plugins and are intended to perform configuration functions for the plugin.
Making your plugin load first
If you need your plugin to be loaded first, you need to pass the parameter "plugins_boot' as follows:
register_elgg_event_handler('plugins_boot', 'system', 'myplugin_init');
Simplepie plugin uses this code in its start.php.
Bypassing Elgg event handler
You can bypass Elgg event handler by calling
myplugin_init()
instead of
register_elgg_event_handler('init', 'system', 'myplugin_init', 1);
This would ensure that myplugin_init() would be called before all other plugins as well as ALL start.php files with don't contain _init() functions. Unless it is very very necessary, avoid calling your functions this way.
Elgg Developer Tools plugin uses this code in its start.php
Difference between plugins_boot and bypassing register_elgg_event_handler()
- SimplePie uses plugins_boot
- Elgg Developer Tools bypasses register_elgg_event_handler
- Tips and Eager Widgets don't contain init() functions in their start.php
- profile and Tinyurl contain init() functions
This is the order the plugins would be called:
Elgg Developer Tools Tips Eager Widgets SimplePie Profile Tinyurl