Hooks system: Difference between revisions

m
no edit summary
Tag: 2017 source edit
mNo edit summary
Tag: 2017 source edit
Line 37: Line 37:
1- Initialize the HookManager object (put this in the beginning of your script, just after includes):
1- Initialize the HookManager object (put this in the beginning of your script, just after includes):


<source 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'));
</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:
Line 53: Line 53:
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 61:
   ... // 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 70:


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 92: Line 92:
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!
Line 111: Line 111:
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 150:
}
}
}  
}  
</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.