Line 2: |
Line 2: |
| | | |
| = Introduction = | | = Introduction = |
− | Hooks are a developer feature available since Dolibarr 3.2. | + | Every business always requires something "extra" or different to the Dolibarr core functionality. |
− | It allows developers to add custom code to Dolibarr core without the need to resort to patching.
| + | 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. |
− | On the contrary of the [[triggers]] (another feature to interact with Dolibarr core code) − which are by definition linked to a business action − hooks can happen anywhere at anytime, they are entry points into the program.
| + | 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. |
− | * Hooks are enabled or not for a "context" (ie the module, eg: "productcard" for products, "invoicecard" for invoices, etc..). It's easy to find them, just search (or grep) for "'''initHooks('''" into php files.
| + | This greatly simplifies the upgrading process by keeping modifications separate from the core code. |
− | * Hooks are parts of code that can be completed or replaced by your own code. You can find all hookable part of code by searching (or grepping) for "'''executeHooks('''".
| |
| | | |
− | = Add a hook to allow to insert code = | + | 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. And of course, this is also how hooks are implemented in every core Dolibarr's modules. | + | 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. |
| | | |
| | | |