K4:Hooks
From In-Portal Developers Guide
| ||
---|---|---|
Статьи в этой категории | ||
Hooks in In-Portal allow to execute any events before or after specific events in the system. A live example of this would be deleting a physical image from the file-system once data record of the image is deleted from the database. This functionality is implemented using "hooks".
Hook - is a an event which will be executed before or after the original event it's been hooked to by defining it in Hooks array in Unit Configuration File. There is no limit on number of hooks that can be setup for one event.
These are the terms that will be used in this article:
Current Unit Config - this a unit config where the Hook is defined Main Event - event which triggers the Hook
Decencies
- hook should work strictly with the dataset of Unit it's been defined in or dataset of Main Event triggered the hook
- hook and Main Event (which triggered the hook) should not be located in the same Event Handler (which means that HookToPrefix and DoPrefix can't the same)
Access to Main Event from inside the hooks can be done using the following code:
$master_event =& $event->MasterEvent;
This will return a link to the object of the Main Event.
Registering a Hook
New Hooks can be registered (added) using the code below:
'Hooks' => Array ( Array ( 'Mode' => hAFTER, 'Conditional' => false, 'HookToPrefix' => '#PARENT#', 'HookToSpecial' => '*', 'HookToEvent' => Array ('OnAfterItemDelete'), 'DoPrefix' => '', 'DoSpecial' => '', 'DoEvent' => 'OnDeleteForeignRelations', ), ),
All listed above keys are required. In case if hooks needs to be temporarily disabled then it's recommended to comment out it's entire definition and not only some parts of it (ie. DoEvent
). All array keys starting with HookTo
are related to definition of Main Event while all keys starting to Do
describe which Event will be called once this hook triggered.
Option Name | Description |
---|---|
Mode (int) | When hook will executed in relation of the Main Event:
|
Conditional (boolean)
|
|
HookToPrefix (string) | Unit prefix we are hooking to. |
HookToSpecial (string) | Special of Unit prefix required for hook to be triggered. |
HookToEvent (array) | List of events which will trigger the hook (before or after their execution). |
DoPrefix (string) | Unit prefix where event triggered by the hook is defined. |
DoSpecial (string) | Special of Unit prefix, that will be passed to triggered Event specified in DoEvent option (see below). |
DoEvent (string) | Event that will be triggered when the hooks is activated. |
Special Value Options
For more flexibility it's recommended to define Hooks with the following special options:
-
HookToPrefix = '#PARENT#'
- use current value of ParentPrefix key from the current Unit Configuration file; -
HookToPrefix = '*'
- any prefix (e.g., will execute for all prefixes registered in the system), available since In-Portal v5.0.0; -
HookToSpecial = '*'
- call this hook with any Special from the Main Event; -
DoPrefix = ''
- the event fromDoEvent
key is located in Event Handler that is defined in current Unit Config file; -
DoSpecial = '*' - use the same Special, that the Main Event was called with;
Translated from: revision 397