Hooks system: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 2: Line 2:


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 entry points 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 "'''initHooks('''"
* Hooks work by context (ie the module, eg: "productcard" for products, "invoicecard" for invoices, etc..). It's easy to find them, just search for "'''initHooks('''" into php files.
* 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 9: Line 9:
= Implementation =
= 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 (add or replace a part of code), you first need to already have defined a module (see the wiki for that), and then you need to do 2 things:


1- add your module to the hooks of the context you want to hook on.
1- add your module to the list of contexts you want to hook on.
This means that when this context will happen, your module hook code will be called.
This means that when a context you hook will happen, your module hook code will be called.
To do that, just edit your /htdocs/core/modules/modYourModuleName.class.php and edit the $this->module_parts variable with something like this:
To do that, just edit your /htdocs/core/modules/modYourModuleName.class.php and edit the $this->module_parts variable with something like this:
<source lang="php">
<source lang="php">
Line 21: Line 21:


Of course change YourModuleName with your own module's name.
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.


'''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.
'''IMPORTANT''': Be careful: do not forget to DISABLE then RENABLE the module in the admin panel to accept the new context, because these constants are only added to the database when enabling the module.




Navigation menu