Changes

no edit summary
Line 3: Line 3:     
Hooks is a developer feature to allow developer to add personalized code intod Dolibarr core code with no need to patch Dolibarr.
 
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.
+
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 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. You can find overloadable functions by searching for "'''executeHooks('''"
 
* 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. You can find overloadable functions by searching for "'''executeHooks('''"
Line 17: Line 17:     
<source lang="php">
 
<source lang="php">
$this->const = array( 0=>array('MAIN_MODULE_YOURMODULENAME_HOOKS', 'chaine', 'productcard:invoicecard:propalcard', 'Hooks list for managing printing functions of the CustomFields module', 0);
+
$this->const = array(
 +
0=>array('MAIN_MODULE_YOURMODULENAME_HOOKS', 'chaine', 'productcard:invoicecard:propalcard',
 +
'Hooks list for managing printing functions of the CustomFields module', 0, 'current', 1)
 +
);
 
</source>
 
</source>
 +
Note: format of a const declaration:
 +
<source lang="php">
 +
$key=>array($const_name,$type,$value,$description,$visible_on_admin_panel=0,$entity='current',$deleteonunactive=0),
 +
$key2=>...
 +
</source>
 +
With $key being a positive integer.
    
Of course change YourModuleName with your own module's name.
 
Of course change YourModuleName with your own module's name.
Line 24: Line 33:  
Then you get the value at the third index, which is a string with contexts separated by a comma.
 
Then you get the value at the third index, which is a string with contexts separated by a comma.
   −
'''IMPORTANT''': Be careful: do not forget to DISABLE then RENABLE the module in the admin panel to accept the new const values, because these constants are only added to the database when enabling the module.
+
'''IMPORTANT''': Be careful: do not forget to DISABLE then RENABLE the module in the admin panel to accept the new const values, because these constants are only added to the database when enabling the module. And please note that the const is updated because the last parameter is set to 1 (which represent $deleteonunactive) which by default is set to 0 to preserve the constants even when the module is disabled.
 
  −
And please note that the const are updated because there is the remove() function (in the same file) that tells Dolibarr to remove the constants when disabling the module, else Dolibarr would not update the constants if they were not removed first (even if the values changed). Your remove() function should be something similar to this:
     −
<source lang="php">
  −
/**
  −
* Function called when module is disabled.
  −
* Remove from database constants, boxes and permissions from Dolibarr database.
  −
* Data directories are not deleted.
  −
* @return    int            1 if OK, 0 if KO
  −
*/
  −
function remove()
  −
{
  −
$sql = array("DELETE FROM ".MAIN_DB_PREFIX."const WHERE name like '%_mymodulename' OR name like '%_mymodulename_%';");
  −
return $this->_remove($sql);
  −
}
  −
</source>
      
2- You will overload a hook (a function) with your own.
 
2- You will overload a hook (a function) with your own.
Line 83: Line 77:  
How to find the available hooks ?  
 
How to find the available hooks ?  
 
Just search for "'''executeHooks('''" and you should easily find all the hooked methods calls.
 
Just search for "'''executeHooks('''" and you should easily find all the hooked methods calls.
 +
 +
= See also =
 +
* [[Triggers]]
 +
* [[Interfaces Dolibarr toward foreign systems]]
 +
* [[Interfaces from foreign systems toward Dolibarr]]
439

edits