Line 5:
Line 5:
* Les Hooks sont des fonctions qui peuvent être surchargées. Vous décidez si votre code s'ajoute à celui de dolibarr ou s'il le remplace. Pour rechercher le code qu'il est possible de surcharger faites une recherche pour "'''executeHooks('''"
* Les Hooks sont des fonctions qui peuvent être surchargées. Vous décidez si votre code s'ajoute à celui de dolibarr ou s'il le remplace. Pour rechercher le code qu'il est possible de surcharger faites une recherche pour "'''executeHooks('''"
+
= Implementation =
+
+
To implement a hook in your own module (so that your module can be ''hooked'' by others), you have two steps to follow.
+
+
These steps must be followed for every php script of your module where you want to implement hooks.
+
+
+
1- Initialize the HookManager object (put this in the beginning of your script, just after includes):
+
+
<source lang="php">
+
// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
+
include_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php');
+
$hookmanager=new HookManager($db);
+
$hookmanager->initHooks(array('context'));
+
</source>
+
+
$hookmanager->initHooks() takes 1 parameter (an array of contexts) and activate the hooks management for this script:
+
+
- ''''context'''' is the module's context as a string. This is simply a indicator that hooking functions can use to detect which modules they want to hook (several different modules can have the same hooks names, but a hooking function may want to hook only one specific module and not the others).
+
+
Note: you can set several contexts in the same script (for example if you want to break the functionnalities of your script into smaller parts, or if you are mixing several modules into one, for example when you do a bridge between modules or third-party application).
+
+
+
+
2- Place hooks wherever you want in your script:
+
+
<source lang="php">
+
$parameters=array();
+
$reshook=$hookmanager->executeHooks('hookname',$parameters,$object,$action); // See description below
+
// Note that $action and $object may have been modified by hook
+
</source>
+
+
'''$hookmanager->executeHooks()''' takes 4 parameters:
+
+
- ''''hookname'''' is the hook's name as a string (can be anything you want, or you can follow the Dolibarr's nomenclatura, look at the list of hooks below). eg: 'formObjectOptions'
+
+
- '''$parameters''' is a custom array to send more custom data to the hook (the hooking function can then process it). Place here anything you want, it can be a file, an array of strings, anything...
+
+
eg:
+
<source lang="php">
+
$parameters=array('file'=>'my/path/to/a/file', 'customnames'=>array('henry','david','john'));
+
</source>
+
+
- '''$object''' is the object you want to pass onto the hooking function, mainly the current module's data (eg: invoice object if it is invoice module, etc..). This can be anything you want, but remember this will be the main component hooking functions will be using.
+
+
- '''$action''' is a string indicating the current action (can be set to null or to something sensible like 'create' or 'edit').
+
+
Note: You will want to redo this step if you want to add multiple hooks at different places in your script.
+
+
Now your module should be hookable, and you can follow the procedure below in '''Usage''' to implement a hooking function that will take advantage of your new hooks.
+
+
Note2: If your hook can't be accessed, try to DISABLE then RENABLE your module for the hooks to be taken in account by Dolibarr (may not be required).
= Utilisation =
= Utilisation =
Line 59:
Line 111:
* '''$action''' désigne l'action à exécuter (par exemple "create", "edit" or "view").
* '''$action''' désigne l'action à exécuter (par exemple "create", "edit" or "view").
−
= Liste des Hooks disponibles =
+
= Liste des Hooks disponibles dans Dolibarr =
−
Trouver les hooks disponibles ?
+
Trouver les hooks disponibles dans Dolibarr ?
−
Faites une recherche sur "'''executeHooks('''" dans le code et vous trouverez facilement toutes les fonctions déjà implémentées.
+
Faites une recherche sur "'''executeHooks('''" dans le code source et vous trouverez facilement toutes les fonctions déjà implémentées.
En voici une liste (non complète):
En voici une liste (non complète):
{{ListOfHooks}}
{{ListOfHooks}}
...
...
+
+
Note: veuillez noter que cette liste peut changer à tout moment dans le futur au fur et à mesure que les hooks et contextes soient implémentés dans Dolibarr, donc si vous voulez vraiment savoir si un hook ou contexte spécifique existe, veuillez chercher directement dans le code source avec la méthode indiquée ci-dessus.
+
+
= Liste des Contexts disponibles dans Dolibarr =
+
Pour trouver les contextes disponibles dans Dolibarr, la procédure est similaire aux hooks.
+
Faites une recherche sur "'''initHooks('''" dans le code source et vous trouverez facilement tous les contextes déjà implémentées.
+
+
En voici une liste (non complète):
+
{{ListOfContexts}}
+
...
+
+
Note: veuillez noter que cette liste peut changer à tout moment dans le futur au fur et à mesure que les hooks et contextes soient implémentés dans Dolibarr, donc si vous voulez vraiment savoir si un hook ou contexte spécifique existe, veuillez chercher directement dans le code source avec la méthode indiquée ci-dessus.
= Voir aussi =
= Voir aussi =