Hooks system

From Dolibarr ERP CRM Wiki
Revision as of 13:54, 19 October 2011 by Eldy (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


== 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. - 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:

1- to add your module to the hooks of the context you want to hook on. This means that when this context (module) will happen, your module will be called. To do that, just edit your /htdocs/includes/modYourModuleName.class.php and edit the $this->const variable with something like this:

Code:

$this->const = array(

                    0=>array('MAIN_MODULE_YOURMODULENAME_HOOKS', 'chaine', 'productcard:invoicecard:propalcard', 'Hooks list for managing printing functions of the CustomFields module', 0; 


Of course change YourModuleName with your own module's name. Don't touch 'chaine' which defines the type of the constant. Then you get the value at the third index, which is a string with contexts separated by a comma.


2- You will overload a hook (a function) with your own.

Create the file /htdocs/yourmodulename/class/actions_yourmodulename.class.php and then put something like this:

Code:

/**

*      \class      actions_yourmodulename 
*      \brief      Example hook file 
*/ 

class ActionsYourModuleName // extends CommonObject {

   /** Overloading the doActions function : replacing the parent's function with the one below 
    *  @param      parameters  meta datas of the hook (context, etc...) 
    *  @param      object             the object you want to process (an invoice if you are in invoice module, a propale in propale's module, etc...) 
    *  @param      action             current action (if set). Generally create or edit or null 
    *  @return       void 
    */ 
   function doActions($parameters, $object, $action) { 
       print_r($parameters); 
       echo "action: ".$action; 
       print_r($object); 

       if ($parameters->context == 'somecontext') { 
         // do something only for the context 'somecontext'} 


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

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