30,997
edits
PolyglotBot (talk | contribs) m (Import interlang links (links to translated versions of this page in other languages) from origin English page "Hooks_system".) |
mNo edit summary Tag: 2017 source edit |
||
| Line 30: | Line 30: | ||
1 -初始化HookManager对象(把它放在脚本的开始,在包含之后): | 1 -初始化HookManager对象(把它放在脚本的开始,在包含之后): | ||
< | <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> | ||
$hookmanager->initHooks() takes 1 parameter (an array of contexts) and activate the hooks management for this script: | $hookmanager->initHooks() takes 1 parameter (an array of contexts) and activate the hooks management for this script: | ||
| Line 46: | Line 46: | ||
2- 放置钩子于你想要引入外部代码之处: | 2- 放置钩子于你想要引入外部代码之处: | ||
< | <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 54: | Line 54: | ||
... // 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> | ||
'''$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 63: | Line 63: | ||
eg: | eg: | ||
< | <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> | ||
- '''$object''' is the object you want to pass onto the hooking function, mainly the current module's data (eg: 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, mainly the current module's data (eg: 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 83: | Line 83: | ||
This means that when a context you hook will happen, your module hook code will be called. | 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: | 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: | ||
< | <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> | ||
Of course change YourModuleName with your own module's name. | Of course change YourModuleName with your own module's name. | ||
| Line 100: | Line 100: | ||
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"> | ||
class ActionsYourModuleName | class ActionsYourModuleName | ||
{ | { | ||
| Line 139: | Line 139: | ||
} | } | ||
} | } | ||
</ | </syntaxHighlight> | ||
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 you access the module/context and you will see the parameters and object and action. | ||