devel:event_handlers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
devel:event_handlers [2013-11-19 00:14] – Klap-in | devel:event_handlers [2023-09-02 13:37] (current) – Klap-in | ||
---|---|---|---|
Line 5: | Line 5: | ||
===== The Event Loop ===== | ===== The Event Loop ===== | ||
- | To begin with, it's helpful to understand how events are processed. To be notified of an event, your code must register to receive the notification. Registering places your request for notification on a list of requests for the event. The list is a first come, first serve array; that is, it's treated as a stack where each next request is pushed on the end of the list. This means it's possible to implement more than one handler for the same event. When more handlers are registered, these will have a unknown, random order. | + | To begin with, it's helpful to understand how events are processed. To be notified of an event, your code must register to receive the notification. Registering places your request for notification on a list of requests for the event. The list is a first-come, first-served |
When DokuWiki processes an action that has an associated event, it does not perform this action immediately. Instead, it triggers an event for this action. For instance, when DokuWiki goes to write a wiki page, it triggers an [[devel: | When DokuWiki processes an action that has an associated event, it does not perform this action immediately. Instead, it triggers an event for this action. For instance, when DokuWiki goes to write a wiki page, it triggers an [[devel: | ||
Line 11: | Line 11: | ||
There are actually two lists: one for before and one for after DokuWiki performs its own action. These are designated by the '' | There are actually two lists: one for before and one for after DokuWiki performs its own action. These are designated by the '' | ||
- | After performing its own action, DokuWiki loops through all the '' | + | After performing its own action, DokuWiki loops through all the '' |
DokuWiki' | DokuWiki' | ||
Line 37: | Line 37: | ||
===== Registering an Event ===== | ===== Registering an Event ===== | ||
- | Event handlers are registered using the '' | + | Event handlers are registered using the [[xref>register_hook()]] function of the [[xref> |
The function definition for '' | The function definition for '' | ||
<code php> | <code php> | ||
void register_hook(string $event, string $advise, | void register_hook(string $event, string $advise, | ||
- | | + | |
</ | </ | ||
* '' | * '' | ||
Line 48: | Line 48: | ||
* '' | * '' | ||
* '' | * '' | ||
+ | * '' | ||
==== Global Scope ==== | ==== Global Scope ==== | ||
To call '' | To call '' | ||
- | the global variable | + | the global variable |
<code php> | <code php> | ||
$EVENT_HANDLER-> | $EVENT_HANDLER-> | ||
</ | </ | ||
- | '' | + | '' |
==== Action plugins ==== | ==== Action plugins ==== | ||
Line 62: | Line 63: | ||
In action plugins, event handlers are registered in the '' | In action plugins, event handlers are registered in the '' | ||
<code php> | <code php> | ||
- | public function register(Doku_Event_Handler | + | use dokuwiki\Extension\EventHandler; |
- | $controller-> | + | ... |
+ | public function register(EventHandler | ||
+ | $controller-> | ||
+ | | ||
} | } | ||
</ | </ | ||
* The event is [[devel: | * The event is [[devel: | ||
- | * The event handler is '' | + | * The event handler is '' |
* The '' | * The '' | ||
* Finally, the handler is to be called **after** DokuWiki formats the page content. | * Finally, the handler is to be called **after** DokuWiki formats the page content. | ||
Line 76: | Line 80: | ||
The handler has this basic form: | The handler has this basic form: | ||
<code php> | <code php> | ||
+ | use dokuwiki\Extension\Event; | ||
+ | ... | ||
/** | /** | ||
- | * @param | + | * @param |
- | * @param mixed $param value provided as fifth argument to register_hook() | + | * @param mixed $param value provided as fifth argument to register_hook() |
| | ||
- | function handler (&$event, $param) { | + | public |
// handler code | // handler code | ||
} | } | ||
</ | </ | ||
- | When Doku_Event_Handler | + | When EventHandler |
((DokuWiki' | ((DokuWiki' | ||
==== The Event Object ==== | ==== The Event Object ==== | ||
- | The authoritative specification for the event object | + | The authoritative specification for the event object [[xref>Event]] is found on the |
[[events# | [[events# | ||
Line 95: | Line 101: | ||
* '' | * '' | ||
* '' | * '' | ||
- | * '' | + | * '' |
* '' | * '' | ||
- | * '' | + | * protected |
- | * '' | + | * protected |
==== Methods ==== | ==== Methods ==== | ||
- '' | - '' | ||
- | * Calling this method from a '' | + | * Calling this method from a '' |
- '' | - '' | ||
- | * Calling this method sets '' | + | * Calling this method sets '' |
- | - '' | + | - '' |
- | * This is a global function, not a method of the '' | + | * This is a global function. Calling this function enables the triggering of a user-defined event. Its parameters are as follows: |
* '' | * '' | ||
* '' | * '' | ||
Line 113: | Line 119: | ||
* '' | * '' | ||
* mixed return value: This is whatever is stored in the event' | * mixed return value: This is whatever is stored in the event' | ||
- | * It's possible to use this function to trigger a standard DokuWiki event, but this would require careful coding and a knowledge of when and how the DokuWiki event is processed. Moreover, triggering a standard event doesn' | + | * It's possible to use this function to trigger a standard DokuWiki event, but this would require careful coding and a knowledge of when and how the DokuWiki event is processed. Moreover, triggering a standard event doesn' |
=====Examples===== | =====Examples===== | ||
Line 119: | Line 125: | ||
=====See also ===== | =====See also ===== | ||
- | | + | |
- | * [[Events]] | + | |
- | * [[Action plugins]] | + | * [[Events |
+ | * Use [[Action plugins]] |
devel/event_handlers.1384816495.txt.gz · Last modified: 2013-11-19 00:14 by Klap-in