Hooks system: Difference between revisions

Jump to navigation Jump to search
no edit summary
(Created page with " == Concepts - On the contrary to the triggers which are by definition linked to an action, hooks can happen anywhere at anytime, they are an entry point into the program. - Ho...")
 
No edit summary
Line 1: Line 1:
{{TemplateDocDevEn}}


Hooks is a developer feature to allow developer to add personalized code intod Dolibarr core code with no need to patch Dolibarr.
On the contrary to the triggers (another feature to interact with Dolibarr code) which are by definition linked to a business action, hooks can happen anywhere at anytime, they are an entry point into the program.
- Hooks work by context (ie the module, eg: "productcard" for products, "invoicecard" for invoices, etc..). It's easy to find them, just search for "'''callHooks('''"
- Hooks are functions that can be overloaded by your own. You decide if your code is added to standard Dolibarr code or if it replace Dolibarr code.




== Concepts
= Implementation =
- On the contrary to the triggers which are by definition linked to an action, hooks can happen anywhere at anytime, they are an entry point into the program.
- Hooks work by context (ie the module, eg: "productcard" for products, "invoicecard" for invoices, etc..). It's easy to find them, just search for "callHooks("
- Hooks are functions that can be overloaded by your own. Eg: a hook called "doActions" can be used by yourself by making a function like this:
 
Code:
function doActions($parameters,$object,$action
}
 
 
where
* $parameters is an array of meta-data about the datas contained by the hook (eg: the context, always accessible by $parameters->context).
* $object is the object that you may work on. Eg: the product if you are in the productcard context.
* $action is the action if one is conveyed (generally "create" or "edit").
 
 
== Implementation


To use a hook (overload a function), you first need to already have defined a module (see the wiki for that), and then you need to do 2 things:
To use a hook (overload a function), you first need to already have defined a module (see the wiki for that), and then you need to do 2 things:
Line 26: Line 15:
To do that, just edit your /htdocs/includes/modYourModuleName.class.php and edit the $this->const variable with something like this:
To do that, just edit your /htdocs/includes/modYourModuleName.class.php and edit the $this->const variable with something like this:


Code:
<source lang="php">
$this->const = array(  
$this->const = array(  
                     0=>array('MAIN_MODULE_YOURMODULENAME_HOOKS', 'chaine', 'productcard:invoicecard:propalcard', 'Hooks list for managing printing functions of the CustomFields module', 0;  
                     0=>array('MAIN_MODULE_YOURMODULENAME_HOOKS', 'chaine', 'productcard:invoicecard:propalcard', 'Hooks list for managing printing functions of the CustomFields module', 0;  
 
</source>


Of course change YourModuleName with your own module's name.
Of course change YourModuleName with your own module's name.
Line 41: Line 29:
Create the file /htdocs/yourmodulename/class/actions_yourmodulename.class.php and then put something like this:
Create the file /htdocs/yourmodulename/class/actions_yourmodulename.class.php and then put something like this:


Code:
<source lang="php">
/**  
/**  
  *      \class      actions_yourmodulename  
  *      \class      actions_yourmodulename  
Line 63: Line 50:
         if ($parameters->context == 'somecontext') {  
         if ($parameters->context == 'somecontext') {  
           // do something only for the context 'somecontext'}  
           // do something only for the context 'somecontext'}  
</source>


where
* $parameters is an array of meta-data about the datas contained by the hook (eg: the context, always accessible by $parameters->context).
* $object is the object that you may work on. Eg: the product if you are in the productcard context.
* $action is the action if one is conveyed (generally "create" or "edit").


Your function should now be called when you access the module/context and you will see the parameters and object and action.
Your function should now be called when you access the module/context and you will see the parameters and object and action.


= List of available Hooks =
How to find the available hooks ? Just search for "executeHooks(" and you should easily find all the hooks.
How to find the available hooks ? Just search for "executeHooks(" and you should easily find all the hooks.

Navigation menu