98
edits
mNo edit summary |
|||
| Line 2: | Line 2: | ||
= Introduction = | = Introduction = | ||
Hooks | Every business always requires something "extra" or different to the Dolibarr core functionality. | ||
While all improvements and extensions to the Dolibarr code should be contributed back via Github to be included in a future version, some modifications will be too specific to be of interest to all. | |||
Hence the Hooks system was introduced in Dolibarr 3.2 onwards. This allows developers to add custom code/features to the Dolibarr core files without the need to modify the core files. | |||
This greatly simplifies the upgrading process by keeping modifications separate from the core code. | |||
= Add a hook | As opposed to [[triggers]] (actions linked to a business event) − hooks are entry points into the program and can modify any part of the program functions. | ||
There are many hooks already built into the code and you may add more. They can be considered a point where the code execution is diverted "out" of the current page and runs your code, then returns back. | |||
Your custom code may add something or modify an object or completely replace an existing code block, depending on the implementation of the hook in that specific instance. | |||
Every hook has a name, eg. "doActions" found in this code: $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); | |||
However the hook name is often not unique, so it must be defined within a "context" name. The context name is which code block contains the hook. | |||
So to use a hook, you need to know its context (its location) and its name. | |||
* The Hook Context name (e.g.: "productcard" for products, "invoicecard" for invoices, etc..). can be found by searching for "'''initHooks('''" in php files. | |||
This will find results such as $hookmanager->initHooks(array('thirdpartycomm','globalcard')); where "thirdpartycomm" is the context. | |||
* The Hook name can be found by searching for "'''executeHooks('''". | |||
This will find results such as $reshook = $hookmanager->executeHooks('addMoreBoxStatsCustomer', $parameters, $object, $action); where "addMoreBoxStatsCustomer" is the Hook name. | |||
= Add a hook to insert code = | |||
To implement a hook in your own module (so that your module can be ''hooked'' by others), you have two steps to follow. | To implement a hook in your own module (so that your module can be ''hooked'' by others), you have two steps to follow. | ||
These steps must be followed for every php script of your module where you want to implement hooks. | These steps must be followed for every php script of your module where you want to implement hooks. This is also how hooks are implemented in Dolibarr core modules. | ||
edits