K4:Hooks
From In-Portal Developers Guide
(→Специальные значения опций: дополнение) |
Current revision (04:15, 28 November 2010) (view source) (Trans.) |
||
(One intermediate revision not shown.) | |||
Line 1: | Line 1: | ||
{{toc | category = Unit Configs | sortkey = 004.001}} | {{toc | category = Unit Configs | sortkey = 004.001}} | ||
- | + | Hooks in In-Portal allow to execute any events before or after specific [[K4:Events|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 - | + | 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 [[K4:Unit Configuration File|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 [[EventHandler:About|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: | ||
<source lang="php"> | <source lang="php"> | ||
$master_event =& $event->MasterEvent; | $master_event =& $event->MasterEvent; | ||
</source> | </source> | ||
- | == | + | 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: | ||
<source lang="php"> | <source lang="php"> | ||
Line 33: | Line 38: | ||
</source> | </source> | ||
- | + | 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. <code>DoEvent</code>). All array keys starting with <code>'''HookTo'''</code> are related to definition of Main Event while all keys starting to <code>'''Do'''</code> describe which Event will be called once this hook triggered. | |
{| class="prettytable" | {| class="prettytable" | ||
- | ! | + | ! Option Name || Description |
|- | |- | ||
- | | {{ConfigProperty|Mode|int}} || | + | | {{ConfigProperty|Mode|int}} || When hook will executed in relation of the Main Event: |
- | * <code>hBEFORE</code> - | + | * <code>hBEFORE</code> - execute before Main Event (for example can be used to stop execution of the Main Event if needed); |
- | * <code>hAFTER</code> - | + | * <code>hAFTER</code> - execute after Main Event (this will executed only if Main Event has been successfully executed). |
|- | |- | ||
| {{ConfigProperty|Conditional|boolean}} | | {{ConfigProperty|Conditional|boolean}} | ||
| | | | ||
- | * ''<code>true</code>'' - | + | * ''<code>true</code>'' - execute ONLY if ''<code>$_REQUEST</code>'' contains data from [[K4:Unit Configs#Prefix|Unit prefix]] specified in [[#DoPrefix|DoPrefix]] option (see below); |
- | * ''<code>false</code>'' - | + | * ''<code>false</code>'' - always execute |
|- | |- | ||
- | | {{ConfigProperty|HookToPrefix|string}} || | + | | {{ConfigProperty|HookToPrefix|string}} || [[K4:Unit Configs#Prefix|Unit prefix]] we are hooking to. |
|- | |- | ||
- | | {{ConfigProperty|HookToSpecial|string}} || Special | + | | {{ConfigProperty|HookToSpecial|string}} || Special of Unit prefix required for hook to be triggered. |
|- | |- | ||
- | | {{ConfigProperty|HookToEvent|array}} || | + | | {{ConfigProperty|HookToEvent|array}} || List of [[K4:Events|events]] which will trigger the hook (before or after their execution). |
|- | |- | ||
- | | {{ConfigProperty|DoPrefix|string}} || | + | | {{ConfigProperty|DoPrefix|string}} || [[K4:Unit Configs#Prefix|Unit prefix]] where event triggered by the hook is defined. |
|- | |- | ||
- | | {{ConfigProperty|DoSpecial|string}} || Special, | + | | {{ConfigProperty|DoSpecial|string}} || Special of Unit prefix, that will be passed to triggered Event specified in [[#DoEvent|DoEvent]] option (see below). |
|- | |- | ||
- | | {{ConfigProperty|DoEvent|string}} || | + | | {{ConfigProperty|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: | |
- | * <code>'''HookToPrefix = '#PARENT#''''</code> - | + | * <code>'''HookToPrefix = '#PARENT#''''</code> - use current value of [[K4:Unit Configs#ParentPrefix|ParentPrefix]] key from the current Unit Configuration file; |
- | * <code>'''HookToPrefix = '*''''</code> - | + | * <code>'''HookToPrefix = '*''''</code> - any prefix (e.g., will execute for all prefixes registered in the system), available since [[K4:Core v 5.0.0|In-Portal v5.0.0]]; |
- | * <code>'''HookToSpecial = '*''''</code> - | + | * <code>'''HookToSpecial = '*''''</code> - call this hook with any Special from the Main Event; |
- | * <code>'''DoPrefix = <nowiki>''</nowiki>'''</code> - | + | * <code>'''DoPrefix = <nowiki>''</nowiki>'''</code> - the event from <code>DoEvent</code> key is located in [[EventHandler:About|Event Handler]] that is defined in current Unit Config file; |
- | * <code>'''DoSpecial = '*'''' - | + | * <code>'''DoSpecial = '*'''' - use the same Special, that the Main Event was called with; |
+ | |||
+ | [[en:{{FULLPAGENAME}}]] | ||
+ | [[ru:K4:Hooks]] | ||
+ | |||
+ | Translated from: [http://guide.in-portal.org/rus/index.php?title=K4:Hooks&oldid=397 revision 397] |
Current revision
| ||
---|---|---|
Статьи в этой категории | ||
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