Changes

m
Line 4: Line 4:  
[[fr:Système_de_Hooks]]
 
[[fr:Système_de_Hooks]]
 
[[es:El_sistema_Hooks]]
 
[[es:El_sistema_Hooks]]
 +
[[de:System_von_Hooks]]
 +
[[en:Hooks_system]]
 
[[zh:钩子系统]]
 
[[zh:钩子系统]]
 
<!-- END interlang links -->
 
<!-- END interlang links -->
 +
[[Category:Hooks]]
    
{{TemplateDocDevEn}}
 
{{TemplateDocDevEn}}
Line 35: Line 38:       −
1- Initialize the HookManager object (put this in the beginning of your script, just after includes):
+
1- Initialize the HookManager object  
   −
<source lang="php">
+
For a page, put this in the beginning of the page (after the include of the main):
 +
 
 +
<syntaxHighlight lang="php">
 
// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
 
// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
 
include_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php');
 
include_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php');
 
$hookmanager=new HookManager($db);
 
$hookmanager=new HookManager($db);
 
$hookmanager->initHooks(array('context'));
 
$hookmanager->initHooks(array('context'));
</source>
+
</syntaxHighlight>
   −
$hookmanager->initHooks() takes 1 parameter (an array of contexts) and activates the hooks management for this script:
+
$hookmanager->initHooks() takes 1 parameter (an array of contexts) and activates the hooks management for this script. '<nowiki/>'''context'''' is a context string. This is simply a indicator that hooking functions can use, to detect in which context they are called. Note that the context 'all' is always valid.
 +
 
 +
Note: you can set several contexts in the same init (for example if you want to have a common context into several pages and want also a specific context dedicated to your page).
   −
- '<nowiki/>'''context'''' is a context string. This is simply a indicator that hooking functions can use, to detect in which context they are called.
+
For a method or function, you can getthe hook manager with
   −
Note: you can set several contexts in the same init (for example if you want to have a common context into several pages and want also a specific context dedicated to your page).
+
<syntaxHighlight lang="php">
 +
global $hookmanager;
 +
</syntaxHighlight>
       
2- Place hooks where you want to allow external code:
 
2- Place hooks where you want to allow external code:
   −
<source lang="php">
+
<syntaxHighlight lang="php">
 
$parameters=array();
 
$parameters=array();
 
$reshook=$hookmanager->executeHooks('hookname',$parameters,$object,$action); // See description below
 
$reshook=$hookmanager->executeHooks('hookname',$parameters,$object,$action); // See description below
Line 61: Line 70:  
   ... // standard code that can be disabled/replaced by hook if return code > 0.
 
   ... // standard code that can be disabled/replaced by hook if return code > 0.
 
}
 
}
</source>
+
</syntaxHighlight>
    
'''$hookmanager->executeHooks()''' takes 4 parameters and add a hook (an entry point in your script for external functions):
 
'''$hookmanager->executeHooks()''' takes 4 parameters and add a hook (an entry point in your script for external functions):
Line 70: Line 79:     
eg:
 
eg:
<source lang="php">
+
<syntaxHighlight lang="php">
 
$parameters=array('file'=>'my/path/to/a/file', 'customnames'=>array('henry','david','john'));
 
$parameters=array('file'=>'my/path/to/a/file', 'customnames'=>array('henry','david','john'));
</source>
+
</syntaxHighlight>
    
- '''$object''' is the object you want to pass onto the hooking function, usually the current module's data (eg: the invoice object if it is invoice module, etc..). This can be anything you want, but remember this will be the main component hooking functions will be using.
 
- '''$object''' is the object you want to pass onto the hooking function, usually the current module's data (eg: the invoice object if it is invoice module, etc..). This can be anything you want, but remember this will be the main component hooking functions will be using.
Line 88: Line 97:  
One the descriptor is created, two further steps are required:
 
One the descriptor is created, two further steps are required:
   −
1- add your module/the core module context name to the list of contexts you want to hook on.
+
1- add your context key 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.
+
This means that when the core program code is executed into 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'''.
 
Edit your '''/htdocs/''yourmodulename''/core/modules/mod''YourModuleName''.class.php''' and add the context name to the variable '''$this->module_parts'''.
 
eg:
 
eg:
<source lang="php">
+
<syntaxHighlight lang="php">
 
$this->module_parts = array(
 
$this->module_parts = array(
 
'hooks' => array('hookcontext1','hookcontext2')  // Set here all hooks context you want to support
 
'hooks' => array('hookcontext1','hookcontext2')  // Set here all hooks context you want to support
 
);
 
);
</source>
+
</syntaxHighlight>
    
Don't forget to change YourModuleName to your own module's name!
 
Don't forget to change YourModuleName to your own module's name!
   −
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:
+
Note: one way to determine the name of contexts you are when you reach a part of code is to add this code in the PHP code:
print('Module context: '.$object->context);
+
<syntaxHighlight lang="php">
 +
print('Module context: '.join(',', $object->contextarray));
 +
</syntaxHighlight>
 +
 
 +
Note that the '''all''' context means you want your hook beeing executed whatever is the context. The '''main''' means you want your hook to be executed for any web page, and '''cli''' means in every Command Line script (even if you can't see this context keys in $object->contextarray).
    
[[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.
 
[[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.
Line 111: Line 124:  
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:
   −
<source lang="php">
+
<syntaxHighlight lang="php">
 
class ActionsYourModuleName
 
class ActionsYourModuleName
 
{  
 
{  
Line 150: Line 163:  
}
 
}
 
}  
 
}  
</source>
+
</syntaxHighlight>
    
Your function should now be called when the program executes 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.
Line 183: Line 196:  
Just search for "'''executeHooks('''" through the whole sourcecode (generally called "find in files") and you should easily find all the hooked methods calls.
 
Just search for "'''executeHooks('''" through the whole sourcecode (generally called "find in files") and you should easily find all the hooked methods calls.
 
There are many, and more are added in each version update.
 
There are many, and more are added in each version update.
This is small list of some example hooks (not up to date/complete):
+
This is small list of some example hooks (not up to date/complete): [[Hooks]]
{{ListOfHooks}}
  −
...
      
Note: please note that this list changes continually as hooks and contexts are added into Dolibarr, so if you really need to find a specific hooks, please search directly in the sourcecode.
 
Note: please note that this list changes continually as hooks and contexts are added into Dolibarr, so if you really need to find a specific hooks, please search directly in the sourcecode.
Line 194: Line 205:     
This is a small list of them (not complete):
 
This is a small list of them (not complete):
 +
 
{{ListOfContexts}}
 
{{ListOfContexts}}
 
...
 
...