钩子系统: Difference between revisions

Jump to navigation Jump to search
Tag: 2017 source edit
Line 12: Line 12:
{{TemplateMainPageZh}}
{{TemplateMainPageZh}}


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


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


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


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


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


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


Line 30: Line 32:
1 -初始化HookManager对象(把它放在脚本的开始,在包含之后):
1 -初始化HookManager对象(把它放在脚本的开始,在包含之后):


<syntaxHighlight 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'));
</syntaxHighlight>
</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:


- ''''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'''' 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).
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).
Line 46: Line 48:
2- 放置钩子于你想要引入外部代码之处:
2- 放置钩子于你想要引入外部代码之处:


<syntaxHighlight 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 54: Line 56:
   ... // 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>
</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):


- ''''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'
- '<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''' 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...
- '''$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:
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''' 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 75: Line 77:
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.
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.


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


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:
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:
Line 83: Line 85:
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">
<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>
</syntaxhighlight>


Of course change YourModuleName with your own module's name.
Of course change YourModuleName with your own module's name.
Line 100: Line 102:
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">
<syntaxhighlight lang="PHP">
class ActionsYourModuleName
class ActionsYourModuleName
{  
{  
Line 139: Line 141:
}
}
}  
}  
</syntaxHighlight>
</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.


'''Where''':
'''Where''':
* '''$parameters''' is an array of meta-data about the datas contained by the hook (eg: the context, always accessible by $parameters['context']).
 
* '''$object''' is the object that you may work on. Eg: the product if you are in the productcard context.
*'''$parameters''' is an array of meta-data about the datas contained by the hook (eg: the context, always accessible by $parameters['context']).
* '''$action''' is the action if one is conveyed (generally "create", "edit" or "view").
*'''$object''' is the object that you may work on. Eg: the product if you are in the productcard context.
* '''$hookmanager''' is propagated only to allow your hook to call another hook.
*'''$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''':
'''Returns''':
* 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.


= 可用钩子列表 =
*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 ?  
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.
Just search for "'''executeHooks('''" through the whole sourcecode (generally called "find in files") and you should easily find all the hooked methods calls.
Line 165: Line 169:
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.
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.


= 可用上下文列表 =
=可用上下文列表=
To find the complete list of available modules' context in Dolibarr's nomenclatura, the procedure is similar to finding hooks.
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.
Search for "'''initHooks('''" through the whole sourcecode (generally called "find in files") and you should find all the possible contexts.
Line 176: Line 180:
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.
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]]
 
* [[Interfaces Dolibarr toward foreign systems]]
*[[Triggers]]
* [[Interfaces from foreign systems toward Dolibarr]]
*[[Interfaces Dolibarr toward foreign systems]]
*[[Interfaces from foreign systems toward Dolibarr]]
714

edits

Navigation menu