钩子系统: Difference between revisions

Jump to navigation Jump to search
Line 25: Line 25:
这将找到诸如“$reshook = $hookmanager->executeHooks('addMoreBoxStatsCustomer', $parameters, $object, $action);”的结果,其中“addMoreBoxStatsCoustomer”是Hook名称。
这将找到诸如“$reshook = $hookmanager->executeHooks('addMoreBoxStatsCustomer', $parameters, $object, $action);”的结果,其中“addMoreBoxStatsCoustomer”是Hook名称。


=添加钩子以允许插入代码=
=添加hook以允许插入代码=
要在自己的模块中实现一个钩子(以便其他模块可以“钩住”),你有两个步骤要遵循。
要在您自己的模块中实现hook(以便您的模块可以被其他模块“钩住”),您需要执行两个步骤。


对于要实现钩子的模块的每个PHP脚本,必须遵循这些步骤。当然,这也是钩子在每个核心Dolibarr模块中实现的方式。
这些步骤必须为模块中要实现hook的每个PHP脚本复制。当然,这也是钩子在每个核心Dolibarr模块中实现的方式。


1 -初始化HookManager对象(把它放在脚本的开始,在包含之后):
1 - 初始化HookManager对象
 
对于一个页面,将这段代码放在PHP脚本的开头(在 main 的 include 之后):


<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
Line 39: Line 41:
</syntaxhighlight>
</syntaxhighlight>


$hookmanager->initHooks() takes 1 parameter (an array of contexts) and activate the hooks management for this script:
$hookmanager->initHooks() 接受1个参数(上下文数组),并启用此脚本的钩子支持。


- '<nowiki/>'''context'''' is a context string. This is simply a indicator that hooking functions can use to detect in which context they are called.
- '<nowiki/>'''context'''' 是包含执行上下文的字符串。这是一个简单的指示符,钩子函数可以使用它来检测何时调用它们(多个页面/模块可以在不同的位置调用同一个钩子,而一个钩子函数可能只想为一个给定的上下文运行,而不想为其他上下文运行)。


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 deidcated to your page).
注意:您可以同时放置多个上下文(例如,如果您需要多个页面共享一个上下文,但也需要特定于给定页面的上下文)。


对于方法或函数,可以通过以下方式获取钩子管理器:


2- 放置钩子于你想要引入外部代码之处:
<syntaxhighlight lang="PHP">
global $hookmanager;
</syntaxhighlight>
2 - 然后将钩子调用放置在允许添加代码的位置:


<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
Line 58: Line 64:
</syntaxhighlight>
</syntaxhighlight>


'''$hookmanager->executeHooks()''' takes 4 parameters and add a hook (an entry point in your script for external functions):
'''$hookmanager->executeHooks()''' 接受4个参数并添加钩子(这是脚本和模块外部函数的脚本入口点):
 
- '<nowiki/>'''hookname'''' 是将被调用的hook的名称(可以是您想要的任何内容,也可以遵循 Dolibarr 的命名法,查看下面的钩子列表)。例如:“formObjectOptions”
 
 


- '<nowiki/>'''hookname'''' is the hook's name as a string (can be anything you want, or you can follow the Dolibarr's nomenclatura, look at the list of hooks below). eg: 'formObjectOptions'
- '''$parameters''' 是一个自定义数组,用于向钩子传递更多自定义数据(钩子中的函数可以处理这些数据)。把你想要的放在这里,它可以是一个文件、一个字符串数组、或任何东西...


- '''$parameters''' is a custom array to send more custom data to the hook (the hooking function can then process it). Place here anything you want, it can be a file, an array of strings, anything...
例如:


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, 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''' 是您要传递给钩子函数的对象,通常是当前模块的数据(例如,如果您在发票模块中,则是发票对象等)。它可以是您想要的任何东西,但请记住,它将是钩子函数使用的主要组件。


- '''$action''' is a string indicating the current action (can be set to null or to something sensible like 'create' or 'edit').
- '''$action''' 是一个指示当前操作的字符串(可以是null或类似于“create”或“edit”的内容)。


Note: You will want to redo this step if you want to add multiple hooks at different places in your script.
注意:如果要在脚本的不同位置添加多个钩子,则需要多次重复此步骤。


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.
现在您的模块应该可以被挂接了,您可以按照下面的'''实现钩子'''中的过程来实现一个钩子函数,该函数将利用您刚才添加的hook(还可以测试它是否有效)。


=实现钩子=
=实现钩子=
714

edits

Navigation menu