Events ###### .. contents:: Contents :local: :depth: 2 Overview ======== Elgg has an event system that can be used to replace or extend core functionality. Plugins influence the system by creating handlers (`callables `_ such as functions and methods) and registering them to handle the events. When an event is triggered, a set of handlers is executed in order of priority. Each handler is passed arguments and has a chance to influence the process. After execution, the "trigger" function returns a value based on the behavior of the handlers. .. seealso:: - :doc:`/guides/events-list` Elgg Events =========== Elgg Events are triggered when an Elgg object is created, updated, or deleted; and at important milestones while the Elgg framework is loading. Examples: a blog post being created or a user logging in. These events are mostly used to notify the rest of the system that something has happened. There are also events that are used to influence output, configuration or behaviour of the system. Each Elgg event has a name and a type (system, user, object, relationship name, annotation, group) describing the type of object passed to the handlers. .. _before-after: Before and After Events ----------------------- Some events are split into "before" and "after". This avoids confusion around the state of the system while in flux. E.g. Is the user logged in during the [login, user] event? Before Events have names ending in ":before" and are triggered before something happens. Handlers can cancel the event by returning ``false``. When ``false`` is returned by a handler, following handlers will not be called. After Events, with names ending in ":after", are triggered after something happened. Handlers **cannot** cancel these events; all handlers will always be called. Where before and after events are available, developers are encouraged to transition to them, though older events will be supported for backwards compatibility. Elgg Event Handlers ------------------- Elgg event handlers are callables: .. code-block:: php getOriginalAttributes()`` no longer available. Invokable classes as handlers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You may use a class with an ``__invoke()`` method as a handler. Just register the class name and it will be instantiated (with no arguments) for the lifetime of the event. .. code-block:: php events->triggerSequence($event, $type, $object, $callable); // or if you wish to have a result sequence $result = elgg->events->triggerResultsSequence($name, $type, $params, $value, $callable); When called with for example ``'cache:clear', 'system'`` the following three events are triggered - ``'cache:clear:before', 'system'`` - ``'cache:clear', 'system'`` - ``'cache:clear:after', 'system'`` Parameters: - **$event** The event name. - **$object_type** The object type (e.g. "user" or "object"). - **$object** The object (e.g. an instance of ``ElggUser`` or ``ElggGroup``) - **$callable** Callable to run on successful event, before event:after .. note:: As of Elgg 6.0 the ``:after`` event will no longer be triggered if the result of the callable is ``false``. This was done in order to prevent the system from thinking something was done which wasn't successful. For example the ``'delete', 'user'`` event sequence. If the callback (which handles the actual removal from the database) wasn't successful the ``:after`` event implied that the user was deleted. Now this is only triggered when the user is actually removed from the database. Unregister Event Handlers ------------------------- The functions ``elgg_unregister_event_handler`` can be used to remove handlers already registered by another plugin or Elgg core. The parameters are in the same order as the registration functions, except there's no priority parameter. .. code-block:: php