4,395
edits
(Created page with "{{TemplateDocDevFr}} {{ToTranslate}} Hooks is a developer feature to allow developer to add personalized code intod Dolibarr core code with no need to patch Dolibarr. On the con...") |
No edit summary |
||
| Line 17: | Line 17: | ||
<source lang="php"> | <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); | ||
</source> | </source> | ||
| Line 25: | Line 24: | ||
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 | '''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 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: | 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"> | <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> | </source> | ||
2- You will overload a hook (a function) with your own. | 2- You will overload a hook (a function) with your own. | ||
Create | Create a file '''/htdocs/''yourmodulename''/class/actions_''yourmodulename''.class.php''' and then put something like this: | ||
<source lang="php"> | <source lang="php"> | ||
class ActionsYourModuleName // extends CommonObject | class ActionsYourModuleName // extends CommonObject | ||
{ | { | ||
| Line 62: | Line 56: | ||
* @return void | * @return void | ||
*/ | */ | ||
function doActions($parameters, $object, $action) { | function doActions($parameters, $object, $action) | ||
{ | |||
print_r($parameters); | print_r($parameters); | ||
echo "action: ".$action; | echo "action: ".$action; | ||
print_r($object); | print_r($object); | ||
if ($parameters->context == 'somecontext') { | if ($parameters->context == 'somecontext') | ||
// do something only for the context 'somecontext'} | { | ||
// do something only for the context 'somecontext' | |||
} | |||
} | |||
} | |||
</source> | </source> | ||
where | where | ||
* $parameters is an array of meta-data about the datas contained by the hook (eg: the context, always accessible by $parameters->context). | * '''$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. | * '''$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", "edit" or "view"). | * '''$action''' is the action if one is conveyed (generally "create", "edit" or "view"). | ||
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 = | = List of available Hooks = | ||
{{ToComplete}} | |||
* '''formObjectOptions''': called everytime fields associated to the main object are printed or inputted (eg: creation form, datasheet, edit form, etc..). | |||
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 | Just search for "'''executeHooks('''" and you should easily find all the hooked methods calls. | ||