Changes

Jump to navigation Jump to search
m
Line 71: Line 71:  
Now your module should be hookable, and you can follow the procedure below in '''Implement a hook''' to implement a hooking function that will take advantage of your new hooks.
 
Now your module should be hookable, and you can follow the procedure below in '''Implement a hook''' to implement a hooking function that will take advantage of your new hooks.
   −
= Implement the Hook =
+
= Implement the Hook functions =
   −
To use a hook (add or replace a part of code), you first need to already have defined a module descriptor (see [[Module_development#Create_your_module_descriptor]]), and then you need to do 2 things:
+
To use a hook (to add to/replace a part of code), you first need to already have defined a module descriptor (see [[Module_development#Create_your_module_descriptor]]). Using a hook function is a part of creating a custom module, so please use the built-in Module Builder to easily create a custom module from a template so you can then modify the relevant hook section.
   −
1- add your module to the list of contexts you want to hook on.
+
One the descriptor is created, two further steps are required:
This means that when a context you hook will happen, your module hook code will be called.
+
 
To do that, just edit your '''/htdocs/''yourmodulename''/core/modules/mod''YourModuleName''.class.php''' and edit the '''$this->module_parts''' variable with something like this:
+
1- add your module/the core module context name to the list of contexts you want to hook on.
 +
This means that when the core program code executes a context, your module hook function will be called.
 +
Edit your '''/htdocs/''yourmodulename''/core/modules/mod''YourModuleName''.class.php''' and add the context name to the variable '''$this->module_parts'''.
 +
eg:
 
<source lang="php">
 
<source lang="php">
 
$this->module_parts = array(
 
$this->module_parts = array(
Line 84: Line 87:  
</source>
 
</source>
   −
Of course change YourModuleName with your own module's name.
+
Don't forget to change YourModuleName to your own module's name!
   −
Note: you can find the current module's context with print('Module context: '.$object->context); (put this inside the module's php file where the hook resides and that you want to hook).
+
Note: one way to determine the context name of the core module that you wish to modify is by adding this code in that modules php code:
 +
print('Module context: '.$object->context);
   −
[[File:warning.png]] Be careful: do not forget to DISABLE then ENABLE the module in the admin panel to accept the new context, because these constants are only added/refreshed to the database when re-enabling your module.
+
[[File:warning.png]] Note that when you add a new context name to the 'hooks' => array, this list of contexts must be stored in the database. This only occurs when the custom module is enabled.
 +
So, when you add/remove/modify/rename any context name, you MUST disable and enable the custom module for the changes to take effect.
      −
2- You will overload a hook (a function) with your own.
+
2- You will override a hook (a function) with your own function.
    
Create a file '''/htdocs/''yourmodulename''/class/actions_''yourmodulename''.class.php''' and then put something like this:
 
Create a file '''/htdocs/''yourmodulename''/class/actions_''yourmodulename''.class.php''' and then put something like this:
Line 99: Line 104:  
{  
 
{  
 
/**
 
/**
* Overloading the doActions function : replacing the parent's function with the one below
+
* Overriding the doActions function : replacing the parent function with the one below
 
*
 
*
 
* @param  array()        $parameters    Hook metadatas (context, etc...)
 
* @param  array()        $parameters    Hook metadatas (context, etc...)
Line 136: Line 141:  
</source>
 
</source>
   −
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 the program executes the module/context and you will see the parameters and object and action.
    
'''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. Note the calling context is stored in  $parameters['context'] which is added to the array when it branches through hookmanager.
* '''$object''' is the object that you may work on. Eg: the product if you are in the productcard context.
+
* '''$object''' is the object/data 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 passed (generally "create", "edit" or "view").
 
* '''$hookmanager''' is propagated only to allow your hook to call another hook.
 
* '''$hookmanager''' is propagated only to allow your hook to call another hook.
    
'''Returns''':
 
'''Returns''':
* The return code from a hook is 0 or 1 if success, negative if error. In general, it will be 0. It can be 1, which means that, in some specific cases, your code hook completely replaces what Dolibarr would do without the hook. If return code is negative, you can also set $this->errors[]='Error message to report to user'
+
* The return code from a hook ($reshook) is 0 / 1 if successful, or negative if in error.
* If the method sets the property $this->results with an array, then the array $hookmanager->resArray will automatically be enriched with the contents of this array, which can be reused later.
+
 
 +
Return Code 0. In this case if the subsequent core module code function is enclosed by
 +
if (empty($reshook)) {...}
 +
it will execute as normal but with the data modified by your custom function.
 +
 
 +
Return Code 1. In this case if the subsequent core module code function is enclosed by
 +
if (empty($reshook)) {...}
 +
it will not execute at all. So this means it has been replaced by your custom  function.
 +
 
 +
Return code is negative if an error has occurred in your function. You can also set $this->errors[]='Error message to report to user'
 +
 
 +
* If the method sets the property $this->results with an array, then the array $hookmanager->resArray will automatically be loaded with the contents of this array, may be reused later.
 
* If the method sets the property $this->resprints with a string, then this string will be displayed by the hook manager (method executeHook), immediately after your method exit.
 
* If the method sets the property $this->resprints with a string, then this string will be displayed by the hook manager (method executeHook), immediately after your method exit.
* Your code can also modify the value of $object and $action.
+
* Your code may also modify the value of $object and $action.
    
= List of available Hooks in Dolibarr =
 
= List of available Hooks in Dolibarr =
98

edits

Navigation menu