|
|
| Line 95: |
Line 95: |
| =实现钩子= | | =实现钩子= |
|
| |
|
| 要使用钩子(即添加或覆盖部分代码),必须首先定义模块描述符文件(请参阅[[模块开发#创建模块描述符(必做)]])。然后,您必须执行以下步骤:
| | 要使用钩子(即添加或覆盖部分代码),必须首先定义模块描述符(请参阅[[模块开发#创建模块描述符(必做)]])。然后,您必须执行以下步骤: |
|
| |
|
| 1- 将您的模块添加到要钩起的上下文列表中。 | | 1 - 将模块添加到钩子应该运行的上下文中。这意味着,在给定的上下文中,将调用您的代码。为此,请编辑模块的描述符('''/htdocs/''yourmodulename''/core/modules/mod''YourModuleName''.class.php'''),并输入变量'''$this->module_parts''',如示例所示: |
|
| |
|
| 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:
| |
| <syntaxhighlight lang="PHP"> | | <syntaxhighlight lang="PHP"> |
| $this->module_parts = array( | | $this->module_parts = array( |
| Line 107: |
Line 105: |
| </syntaxhighlight> | | </syntaxhighlight> |
|
| |
|
| Of course change YourModuleName with your own module's name.
| | 不要忘记将 YourModuleName 更改为您自己的模块名称! |
|
| |
|
| Note: you can find the current module's context with print('Module context: '.$object->context); (put this inside the module's php file where the hook resides and that you want to hook).
| | 注意:可以通过添加以下内容来查找模块的上下文: |
|
| |
|
| [[File:warning.png]] Be careful: do not forget to DISABLE then ENABLE the module in the admin panel to accept the new context, because these constants are only added/refreshed to the database when re-enabling your module.
| | <source lang="php"> |
| | print('Module context: '.$object->context); |
| | </source> |
|
| |
|
| | (将这段代码添加到钩子调用所在的PHP文件中,并在检索到上下文值后将其删除)。 |
|
| |
|
| 2- 你将用自己的一个钩子(一个函数)重载。 | | [[File:warning.png]] 警告:不要忘记在模块管理界面中禁用和重新激活模块,以便使更改生效。因为当您将新的上下文名称添加到 <syntaxhighlight lang="PHP"> |
| | $this->module_parts = array( |
| | 'hooks' => array('hookcontext1','hookcontext2') // Set here all hooks context you want to support |
| | ); |
| | </syntaxhighlight> 时,此上下文列表必须存储在数据库中。仅当启用自定义模块时,才会发生这种情况。 因此,当您添加/删除/修改/重命名任何上下文名称时,您必须禁用并启用自定义模块才能使更改生效。 |
| | |
| | 2 - 用您的函数替换现有函数(重载) |
|
| |
|
| Create a file '''/htdocs/''yourmodulename''/class/actions_''yourmodulename''.class.php''' and then put something like this:
| | 在模块中创建 '''/htdocs/''yourmodulename''/class/actions_''yourmodulename''.class.php''',代码包含钩子调用的方法(该方法的名称在调用executeHooks时可见)。下面是一个例子: |
|
| |
|
| <syntaxhighlight lang="PHP"> | | <syntaxhighlight lang="PHP"> |
| Line 159: |
Line 166: |
| </syntaxhighlight> | | </syntaxhighlight> |
|
| |
|
| Your function should now be called when you access the module/context and you will see the parameters and object and action.
| | 然后,在调用包含executeHooks的代码时,将自动调用该方法,executeHooks为您的代码提供参数 $parameters、$Object和$action。 |
| | |
| | '''参数:''' |
| | |
| | *'''$parameters''' 是一个集成钩子数据的元数据数组(其上下文可通过 $parameters['context'] 访问,但根据具体情况,其他信息可能可用)。 |
| | *'''$object''' 是您要处理的对象(例如,productcard上下文中的product) |
| | *'''$action''' 指定要执行的操作(例如 "create", "edit" or "view")。 |
| | *'''$hookmanager''' 传递它只是为了让您的钩子可以调用其他钩子。 |
| | |
| | '''返回值:''' |
| | |
| | *执行成功时挂钩的返回代码必须为0或1;错误时为负数。 |
|
| |
|
| '''Where''':
| | 返回0。如果后续的核心模块代码被包裹于 if (empty($reshook)) {...},它将正常执行,但数据由自定义函数修改。 |
|
| |
|
| *'''$parameters''' is an array of meta-data about the datas contained by the hook (eg: the context, always accessible by $parameters['context']).
| | 返回1。如果后续的核心模块代码被包裹于 if (empty($reshook)) {...},它根本不会执行。这意味着你的钩子所做的完全取代了Dolibarr在调用钩子后所做的。 |
| *'''$object''' is the object that you may work on. Eg: the product if you are in the productcard context.
| |
| *'''$action''' is the action if one is conveyed (generally "create", "edit" or "view").
| |
| *'''$hookmanager''' is propagated only to allow your hook to call another hook.
| |
|
| |
|
| '''Returns''': | | 返回负数。则可以通过设置 $this->errors[]='Error message to report to user' 向用户提供错误消息。 |
|
| |
|
| *The return code from a hook is 0 or 1 if success, negative if error. In general, it will be 0. It can be 1, which means that, in some specific cases, your code hook completely replaces what Dolibarr would do without the hook. If return code is negative, you can also set $this->errors[]='Error message to report to user' | | *如果该方法将一个数组赋给属性 $this->results,则数组 $hookManager->resArray 将自动加载该数组的内容,以便以后重用。 |
| *If the method sets the property $this->results with an array, then the array $hookmanager->resArray will automatically be enriched with the contents of this array, which can be reused later.
| | *如果该方法将一个字符串赋给属性 $this->resprints ,则该字符串将在方法退出时由挂接处理程序(executeHook)打印出来。 |
| *If the method sets the property $this->resprints with a string, then this string will be displayed by the hook manager (method executeHook), immediately after your method exit. | | *您的钩子还可以更改$object和$action的值。 |
| *Your code can also modify the value of $object and $action. | |
|
| |
|
| =可用钩子列表= | | =可用钩子列表= |