钩子系统: Difference between revisions

No edit summary
Tag: 2017 source edit
 
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<!-- BEGIN interlang links -->
<!-- Do NOT edit this section
    Links below are automatically managed by PolyglotBot
    You can edit links on the English source page : Hooks_system -->
[[en:Hooks_system]]
[[fr:Système_de_Hooks]]
[[es:El_sistema_Hooks]]
<!-- END interlang links -->
[[Category:Hooks]]
[[Category:开发]]
{{Template开发文档}}
{{Template开发文档}}
{{Template用户手册}}
{{Template用户手册}}
{{TemplateMainPageZh}}
{{TemplateMainPageZh}}


= 介绍 =
=简介=
钩子开发功能,自Dolibarr 3.2开始启用。
Hooks 是一项开发功能,允许开发人员将自定义代码添加到Dolibarr的标准页面,而无需修改Dolibarr的核心文件。与链接到Dolibarr事件的 [[触发系统]](与Dolibarr代码交互的另一种方式)不同,Hooks可以预埋在Dolibarr核心代码的任何时间、任何地点运行。这些是程序中的插入点(被视为代码执行从当前页面“转移”并运行自定义代码,然后返回的点)。


允许开发者添加代码到Dolibarr核心区域,而不需要诉诸于修改核心代码。
每个Hook都有一个名字,例如,代码:$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action)中的“doActions”;然而,Hook的名称通常不是唯一的(可能在各种代码模块中重复使用),因此为了准确识别位置,代码模块具有一个“上下文”名称,该名称将被该代码模块位置中的所有Hooks引用。因此,要使用Hooks,您需要知道它的上下文名称(位置)和Hooks名称。


与[[触发器]](与Dolibarr核心代码交互的另一个特性)相反-根据定义,它们链接到业务动作,钩子可以在任何时间任何地方发生,它们是程序的入口点。
*Hook是否处于活动状态取决于上下文(通常每个模块有一个上下文:例如,产品模块的“productcard”,发票模块的“invoicecard”等等)。要查找现有的Hooks,请在php文件中搜索“'''initHooks('''”。


* 钩子的开关位于“内容”(即模块,例如:产品的产品卡,发票的发票卡等)。很容易找到它们,只需搜索(或定位)到PHP文件中的"'''initHooks('''"。
这将找到诸如“$hookmanager->initHooks(array('thirdpartycomm','globalcard'));”的结果,其中“thirdpartycomm”是上下文名称


* 钩子是可以由自己的代码完成或替换的代码的一部分。您可以通过搜索(或定位)查找"'''executeHooks('''"来找出代码的“可执行”部分。
*Hooks是用于插入或替换标准代码的函数。要查找可以覆盖的代码,请搜索“'''executeHooks('''”。找不到就可能没有预埋Hook,所以你可以添加自己的Hook(并在Github中提交相应的拉取请求,以便将其包含在未来的版本中)。


= 添加钩子以允许插入代码 =
这将找到诸如“$reshook = $hookmanager->executeHooks('addMoreBoxStatsCustomer', $parameters, $object, $action);”的结果,其中“addMoreBoxStatsCoustomer”是Hook名称。
要在自己的模块中实现一个钩子(以便其他模块可以“钩住”),你有两个步骤要遵循。


对于要实现钩子的模块的每个PHP脚本,必须遵循这些步骤。当然,这也是钩子在每个核心Dolibarr模块中实现的方式。
=添加/预埋hook点以允许插入代码=
要在您自己的模块中预埋hook(以便您的模块可以被其他模块“挂接”),您需要执行两个步骤。


1 -初始化HookManager对象(把它放在脚本的开始,在包含之后):
这些步骤必须为模块中要预埋hook的每个PHP脚本复制。当然,这也是hook在每个核心Dolibarr模块中实现的方式。


<source lang="php">
1 - 初始化HookManager对象
 
对于一个页面,将这段代码放在PHP脚本的开头(在 main 的 include 之后):
 
<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() 接受1个参数(上下文数组),并启用此脚本的钩子支持。


$hookmanager->initHooks() takes 1 parameter (an array of contexts) and activate the hooks management for this script:
- '<nowiki/>'''context'''' 是包含执行上下文的字符串。这是一个简单的指示符,钩子函数可以使用它来检测何时调用它们(多个页面/模块可以在不同的位置调用同一个钩子,而一个钩子函数可能只想为一个给定的上下文运行,而不想为其他上下文运行)。


- ''''context'''' is a context string. This is simply a indicator that hooking functions can use to detect in which context they are called.
注意:您可以同时放置多个上下文(例如,如果您需要多个页面共享一个上下文,但也需要特定于给定页面的上下文)。


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).
对于方法或函数,可以通过以下方式获取钩子管理器:


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


2- 放置钩子于你想要引入外部代码之处:
2 - 然后将<syntaxhighlight lang="PHP">
$parameters=array();
$reshook=$hookmanager->executeHooks('hookname',$parameters,$object,$action); // See description below
// Note that $action and $object may have been modified by hook
if (empty($reshook))
{
  ... // standard code that can be disabled/replaced by hook if return code > 0.
}
</syntaxhighlight>调用放置在允许添加代码的位置:


<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 45: Line 74:
   ... // 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()''' 接受4个参数并添加钩子(这是脚本和模块外部函数的脚本入口点):


'''$hookmanager->executeHooks()''' takes 4 parameters and add a hook (an entry point in your script for external functions):
- '<nowiki/>'''hookname'''' 是将被调用的hook的名称(可以是您想要的任何内容,也可以遵循 Dolibarr 的命名法,查看下面的钩子列表)。例如:“formObjectOptions”


- ''''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">
<source 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, 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(还可以测试它是否有效)。


= 实现钩子 =
=实现Hook=


To use a hook (add or replace a part of code), you first need to already have defined a module descriptor (see [[Module_development#Create_your_module_descriptor]]), and then you need to do 2 things:
要使用钩子(即添加或覆盖部分代码),必须首先定义模块描述符(请参阅[[模块开发#创建模块描述符(必做)]])。然后,您必须执行以下步骤:


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.
<syntaxhighlight lang="PHP">
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:
<source 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>


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文件中,并在检索到上下文值后将其删除)。
[[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- 你将用自己的一个钩子(一个函数)重载。
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时可见)。下面是一个例子:


<source lang="php">
<syntaxhighlight lang="PHP">
class ActionsYourModuleName
class ActionsYourModuleName
{  
{  
Line 130: Line 167:
}
}
}  
}  
</source>
</syntaxhighlight>
 
然后,在调用包含executeHooks的代码时,将自动调用该方法,executeHooks为您的代码提供参数 $parameters、$Object和$action。
 
'''参数:'''
 
*'''$parameters''' 是一个集成钩子数据的元数据数组(其上下文可通过 $parameters['context'] 访问,但根据具体情况,其他信息可能可用)。
*'''$object''' 是您要处理的对象(例如,productcard上下文中的product)
*'''$action''' 指定要执行的操作(例如 "create", "edit" or "view")。
*'''$hookmanager''' 传递它只是为了让您的钩子可以调用其他钩子。
 
'''返回值:'''
 
*执行成功时挂钩的返回代码必须为0或1;错误时为负数。
 
返回0。如果后续的核心模块代码被包裹于 if (empty($reshook)) {...},它将正常执行,但数据由自定义函数修改。
 
返回1。如果后续的核心模块代码被包裹于 if (empty($reshook)) {...},它根本不会执行。这意味着你的钩子所做的完全取代了Dolibarr在调用钩子后所做的。
 
返回负数。则可以通过设置 $this->errors[]='Error message to report to user' 向用户提供错误消息。


Your function should now be called when you access the module/context and you will see the parameters and object and action.
*如果该方法将一个数组赋给属性 $this->results,则数组 $hookManager->resArray 将自动加载该数组的内容,以便以后重用。
*如果该方法将一个字符串赋给属性 $this->resprints ,则该字符串将在方法退出时由挂接处理程序(executeHook)打印出来。
*您的钩子还可以更改$object和$action的值。


'''Where''':
=Dolibarr中可用的Hooks清单=
* '''$parameters''' is an array of meta-data about the datas contained by the hook (eg: the context, always accessible by $parameters['context']).
要查找Dolibarr中可用的挂钩点,只需在源代码中搜索“'''executeHooks('''”即可,您将很容易找到所有已预埋的挂钩点。
* '''$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''':
以下是一个可用的挂钩点清单(不完整):[[:Category:Hooks|Category:Hooks]]...
* 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'
* 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.
* 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.
* Your code can also modify the value of $object and $action.


= 可用钩子列表  =
请注意:此清单会随着版本升级而不断增长,因此如果您真的想知道是否存在特定的钩子或上下文,请使用上面列出的方法直接搜索源代码。
How to find a complete list of all available hooks in Dolibarr's nomenclatura ?
Just search for "'''executeHooks('''" through the whole sourcecode (generally called "find in files") and you should easily find all the hooked methods calls.


This is however a small list of them (not complete):
=Dolibarr中可用的上下文清单=
{{ListOfHooks}}
要查找Dolibarr中可用的上下文,查找过程类似于查找挂钩点。
...


Note: please note that this list may change in the future as hooks and contexts are added into Dolibarr, so if you really need to find a specific hooks, please search directly in the sourcecode.
在源代码中搜索“'''initHooks('''”,您将很容易找到所有已实现的上下文。


= 可用上下文列表 =
这是其中的一小部分(不完整):
To find the complete list of available modules' context in Dolibarr's nomenclatura, the procedure is similar to finding hooks.
Search for "'''initHooks('''" through the whole sourcecode (generally called "find in files") and you should find all the possible contexts.


This is however a small list of them (not complete):
{{ListOfContexts}}
{{ListOfContexts}}
...
...


Note: please note that this list may change in the future as hooks and contexts are added into Dolibarr, so if you really need to find a specific hooks, please search directly in the sourcecode.
请注意:此清单会随着版本升级而不断增长,因此如果您真的想知道是否存在特定的钩子或上下文,请使用上面列出的方法直接搜索源代码。
 
=参考=


= 参考 =
*[[触发系统|触发器]]
* [[Triggers]]
*[[Dolibarr到外部系统的接口]]
* [[Interfaces Dolibarr toward foreign systems]]
*[[外部系统到Dolibarr的接口]]
* [[Interfaces from foreign systems toward Dolibarr]]

Latest revision as of 01:56, 9 October 2023


简介

Hooks 是一项开发功能,允许开发人员将自定义代码添加到Dolibarr的标准页面,而无需修改Dolibarr的核心文件。与链接到Dolibarr事件的 触发系统(与Dolibarr代码交互的另一种方式)不同,Hooks可以预埋在Dolibarr核心代码的任何时间、任何地点运行。这些是程序中的插入点(被视为代码执行从当前页面“转移”并运行自定义代码,然后返回的点)。

每个Hook都有一个名字,例如,代码:$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action)中的“doActions”;然而,Hook的名称通常不是唯一的(可能在各种代码模块中重复使用),因此为了准确识别位置,代码模块具有一个“上下文”名称,该名称将被该代码模块位置中的所有Hooks引用。因此,要使用Hooks,您需要知道它的上下文名称(位置)和Hooks名称。

  • Hook是否处于活动状态取决于上下文(通常每个模块有一个上下文:例如,产品模块的“productcard”,发票模块的“invoicecard”等等)。要查找现有的Hooks,请在php文件中搜索“initHooks(”。

这将找到诸如“$hookmanager->initHooks(array('thirdpartycomm','globalcard'));”的结果,其中“thirdpartycomm”是上下文名称

  • Hooks是用于插入或替换标准代码的函数。要查找可以覆盖的代码,请搜索“executeHooks(”。找不到就可能没有预埋Hook,所以你可以添加自己的Hook(并在Github中提交相应的拉取请求,以便将其包含在未来的版本中)。

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

添加/预埋hook点以允许插入代码

要在您自己的模块中预埋hook(以便您的模块可以被其他模块“挂接”),您需要执行两个步骤。

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

1 - 初始化HookManager对象

对于一个页面,将这段代码放在PHP脚本的开头(在 main 的 include 之后):

// 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');
$hookmanager=new HookManager($db);
$hookmanager->initHooks(array('context'));

$hookmanager->initHooks() 接受1个参数(上下文数组),并启用此脚本的钩子支持。

- 'context' 是包含执行上下文的字符串。这是一个简单的指示符,钩子函数可以使用它来检测何时调用它们(多个页面/模块可以在不同的位置调用同一个钩子,而一个钩子函数可能只想为一个给定的上下文运行,而不想为其他上下文运行)。

注意:您可以同时放置多个上下文(例如,如果您需要多个页面共享一个上下文,但也需要特定于给定页面的上下文)。

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

global $hookmanager;

2 - 然后将

$parameters=array();
$reshook=$hookmanager->executeHooks('hookname',$parameters,$object,$action); // See description below
// Note that $action and $object may have been modified by hook
if (empty($reshook))
{
   ... // standard code that can be disabled/replaced by hook if return code > 0.
}

调用放置在允许添加代码的位置:

$parameters=array();
$reshook=$hookmanager->executeHooks('hookname',$parameters,$object,$action); // See description below
// Note that $action and $object may have been modified by hook
if (empty($reshook))
{
   ... // standard code that can be disabled/replaced by hook if return code > 0.
}

$hookmanager->executeHooks() 接受4个参数并添加钩子(这是脚本和模块外部函数的脚本入口点):

- 'hookname' 是将被调用的hook的名称(可以是您想要的任何内容,也可以遵循 Dolibarr 的命名法,查看下面的钩子列表)。例如:“formObjectOptions”

- $parameters 是一个自定义数组,用于向钩子传递更多自定义数据(钩子中的函数可以处理这些数据)。把你想要的放在这里,它可以是一个文件、一个字符串数组、或任何东西...

例如:

$parameters=array('file'=>'my/path/to/a/file', 'customnames'=>array('henry','david','john'));

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

- $action 是一个指示当前操作的字符串(可以是null或类似于“create”或“edit”的内容)。

注意:如果要在脚本的不同位置添加多个钩子,则需要多次重复此步骤。

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

实现Hook

要使用钩子(即添加或覆盖部分代码),必须首先定义模块描述符(请参阅模块开发#创建模块描述符(必做))。然后,您必须执行以下步骤:

1 - 将模块添加到钩子应该运行的上下文中。这意味着,在给定的上下文中,将调用您的代码。为此,请编辑模块的描述符(/htdocs/yourmodulename/core/modules/modYourModuleName.class.php),并输入变量$this->module_parts,如示例所示:

$this->module_parts = array(
'hooks' => array('hookcontext1','hookcontext2')  // Set here all hooks context you want to support
);

不要忘记将 YourModuleName 更改为您自己的模块名称!

注意:可以通过添加以下内容来查找模块的上下文:

print('Module context: '.$object->context);

(将这段代码添加到钩子调用所在的PHP文件中,并在检索到上下文值后将其删除)。

  警告:不要忘记在模块管理界面中禁用和重新激活模块,以便使更改生效。因为当您将新的上下文名称添加到

$this->module_parts = array(
'hooks' => array('hookcontext1','hookcontext2')  // Set here all hooks context you want to support
);

时,此上下文列表必须存储在数据库中。仅当启用自定义模块时,才会发生这种情况。 因此,当您添加/删除/修改/重命名任何上下文名称时,您必须禁用并启用自定义模块才能使更改生效。

2 - 用您的函数替换现有函数(重载)

在模块中创建 /htdocs/yourmodulename/class/actions_yourmodulename.class.php,代码包含钩子调用的方法(该方法的名称在调用executeHooks时可见)。下面是一个例子:

class ActionsYourModuleName
{ 
	/**
	 * Overloading the doActions function : replacing the parent's function with the one below
	 *
	 * @param   array()         $parameters     Hook metadatas (context, etc...)
	 * @param   CommonObject    &$object        The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
	 * @param   string          &$action        Current action (if set). Generally create or edit or null
	 * @param   HookManager     $hookmanager    Hook manager propagated to allow calling another hook
	 * @return  int                             < 0 on error, 0 on success, 1 to replace standard code
	 */
	function doActions($parameters, &$object, &$action, $hookmanager)
	{
		$error = 0; // Error counter
		$myvalue = 'test'; // A result value

		print_r($parameters);
		echo "action: " . $action;
		print_r($object);

		if (in_array('somecontext', explode(':', $parameters['context'])))
		{
		  // do something only for the context 'somecontext'
		}

		if (! $error)
		{
			$this->results = array('myreturn' => $myvalue);
			$this->resprints = 'A text to show';
			return 0; // or return 1 to replace standard code
		}
		else
		{
			$this->errors[] = 'Error message';
			return -1;
		}
	}
}

然后,在调用包含executeHooks的代码时,将自动调用该方法,executeHooks为您的代码提供参数 $parameters、$Object和$action。

参数:

  • $parameters 是一个集成钩子数据的元数据数组(其上下文可通过 $parameters['context'] 访问,但根据具体情况,其他信息可能可用)。
  • $object 是您要处理的对象(例如,productcard上下文中的product)
  • $action 指定要执行的操作(例如 "create", "edit" or "view")。
  • $hookmanager 传递它只是为了让您的钩子可以调用其他钩子。

返回值:

  • 执行成功时挂钩的返回代码必须为0或1;错误时为负数。

返回0。如果后续的核心模块代码被包裹于 if (empty($reshook)) {...},它将正常执行,但数据由自定义函数修改。

返回1。如果后续的核心模块代码被包裹于 if (empty($reshook)) {...},它根本不会执行。这意味着你的钩子所做的完全取代了Dolibarr在调用钩子后所做的。

返回负数。则可以通过设置 $this->errors[]='Error message to report to user' 向用户提供错误消息。

  • 如果该方法将一个数组赋给属性 $this->results,则数组 $hookManager->resArray 将自动加载该数组的内容,以便以后重用。
  • 如果该方法将一个字符串赋给属性 $this->resprints ,则该字符串将在方法退出时由挂接处理程序(executeHook)打印出来。
  • 您的钩子还可以更改$object和$action的值。

Dolibarr中可用的Hooks清单

要查找Dolibarr中可用的挂钩点,只需在源代码中搜索“executeHooks(”即可,您将很容易找到所有已预埋的挂钩点。

以下是一个可用的挂钩点清单(不完整):Category:Hooks...

请注意:此清单会随着版本升级而不断增长,因此如果您真的想知道是否存在特定的钩子或上下文,请使用上面列出的方法直接搜索源代码。

Dolibarr中可用的上下文清单

要查找Dolibarr中可用的上下文,查找过程类似于查找挂钩点。

在源代码中搜索“initHooks(”,您将很容易找到所有已实现的上下文。

这是其中的一小部分(不完整):

adherents\card.php(111): membercard
adherents\type.php(73): membertypecard
categories\categorie.php(96): categorycard
comm\card.php(72): commcard
comm\propal.php(99): propalcard
comm\action\card.php(85): actioncard
comm\action\index.php(112): agenda
comm\mailing\card.php(55): mailingcard
commande\card.php(93): ordercard
compta\facture.php(105): invoicecard
compta\paiement.php(70): paiementcard
compta\deplacement\card.php(50): tripsandexpensescard
compta\dons\card.php(53): doncard
compta\localtax\clients.php(172): externalbalance
compta\salaries\card.php(47): salarycard
compta\tva\card.php(45): taxvatcard
contact\card.php(77): contactcard
contrat\card.php(70): contractcard
expedition\card.php(85): expeditioncard
fichinter\card.php(80): interventioncard
fourn\card.php(54): suppliercard
fourn\commande\card.php(80): ordersuppliercard
fourn\commande\orderstoinvoice.php(88): orderstoinvoicesupplier
fourn\facture\card.php(72): invoicesuppliercard
fourn\facture\paiement.php(71): paymentsupplier
livraison\card.php(68): deliverycard
product\card.php(91): productcard
product\composition\card.php(55): productcompositioncard
product\fournisseurs.php(62): pricesuppliercard
product\stats\commande.php(45): productstatsorder
product\stats\commande_fournisseur.php(45): productstatssupplyorder
product\stats\contrat.php(45): productstatscontract
product\stats\facture.php(48): productstatsinvoice
product\stats\facture_fournisseur.php(47): productstatssupplyinvoice
product\stats\propal.php(45): productstatspropal
product\stock\card.php(54): warehousecard
projet\card.php(48): projectcard
projet\tasks.php(67): projecttaskcard
resource\card.php(60): resource_card
resource\element_resource.php(58): element_resource
societe\agenda.php(41): agendathirdparty
societe\commerciaux.php(40): salesrepresentativescard
societe\consumption.php(80): consumptionthirdparty
societe\info.php(41): infothirdparty
societe\soc.php(80): thirdpartycard
user\card.php(93): usercard
user\list.php(72): userlist
user\passwordforgotten.php(56): passwordforgottenpage
...

请注意:此清单会随着版本升级而不断增长,因此如果您真的想知道是否存在特定的钩子或上下文,请使用上面列出的方法直接搜索源代码。

参考