Hooks system: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
Tag: 2017 source edit
mNo edit summary
Tags: Visual edit Mobile edit Mobile web edit Advanced mobile edit
 
Line 42: Line 42:
For a page, put this in the beginning of the page (after the include of the main):
For a page, put this in the beginning of the page (after the include of the main):


<syntaxHighlight lang="php">
<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'));
</syntaxHighlight>
</syntaxhighlight>


$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.
$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.
Line 53: Line 53:
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).
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).


For a method or function, you can getthe hook manager with
For a method or function, you can get the hook manager with


<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
global $hookmanager;
global $hookmanager;
</syntaxHighlight>
</syntaxhighlight>




2- Place hooks where you want to allow external code:
2- Place hooks where you want to allow external code:


<syntaxHighlight 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 70: 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.
}
}
</syntaxHighlight>
</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 79: Line 79:


eg:
eg:
<syntaxHighlight 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'));
</syntaxHighlight>
</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 101: Line 101:
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:
<syntaxHighlight 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
);
);
</syntaxHighlight>
</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 name of contexts you are when you reach a part of code is to add this code in the 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:
<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
print('Module context: '.join(',', $object->contextarray));
print('Module context: '.join(',', $object->contextarray));
</syntaxHighlight>
</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).
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).
Line 124: 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:


<syntaxHighlight lang="php">
<syntaxhighlight lang="PHP">
class ActionsYourModuleName
class ActionsYourModuleName
{  
{  
Line 163: Line 163:
}
}
}  
}  
</syntaxHighlight>
</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.

Navigation menu