Sugarcrm支持产品指南糖开发人员糖开发人员指南10.1建筑学逻辑钩子
此版本仅适用于Sugarcloud客户。

逻辑钩子

概述

逻辑挂钩框架允许您将动作追加到系统事件,例如在创建,编辑和删除记录时。

钩定义

逻辑挂钩定义文件定义触发事件时要执行的各种操作。值得注意的是,有各种方法可以实现逻辑钩。以下部分描述了实现逻辑挂钩以及何时使用的不同方式。

方法论

逻辑挂钩定义可以涉及特定模块或整个应用程序。无论哪种方式,您必须确定逻辑挂钩定义是否将实现为模块或应用程序的扩展名。以下部分解释了这些方法之间的差异。

模块扩展挂钩

Module extension hooks are located in 。/风俗/Extension/modules/<module>/Ext/LogicHooks/ and allow a developer to define hook actions that will be executed for the specified events in a given module. Extensions are best used when creating customizations that may be installed in various environments. They help prevent conflicting edits that occur when modifying 。/风俗/modules/<module>/logic_hooks.php. More information can be found in the 逻辑钩子 extension section.

模块钩子

Module hooks are located in 。/风俗/modules/<module>/logic_hooks.php and allow a developer to define hook actions that will be executed for the specified events in a given module. This path can be used for private development, however, it is not recommended for use with distributed modules and plugins. For distributed code, please refer to using 模块扩展.

应用程序扩展钩

Application extension hooks are located in 。/风俗/Extension/application/Ext/LogicHooks/ and allow a developer to define hook actions that will be executed for all specified application-level events using the extension framework. More information can be found in the 逻辑钩子 extension section.

应用钩子

Application hooks are located in 。/风俗/modules/logic_hooks.php and allow a developer to define hook actions that will be executed for the specified events in all modules. This path can be used for private development, however, it is not recommended for use with distributed modules and plugins. For distributed code, please refer to using 应用扩展名.

定义属性

All logic hooks must have a $hook_version and $hook_array variable defined. The following sections cover each required variable.

hook_version.

All logic hook definitions will define a $hook_version. This determines the version of the logic hook framework. Currently, the only supported hook version is 1.

$hook_version = 1;

Hook_Array.

The logic hook definition will also contain a $hook_array. This defines the specific action to execute. The hook array will be defined as follows:

姓名 类型 描述
event_name. 细绳 追加操作的事件的名称
process_index. 整数 行动执行顺序
描述 细绳 钩子动作的简短描述
文件_path 细绳 The path to the logic hook file in the 。/风俗/ directory, or null if using namespaces
班级名称 细绳 逻辑钩操作类的名称,包括任何名称空间
method_name. 细绳 逻辑挂钩操作方法的名称

您的定义应该类似于下面的代码块:

<?php

    $hook_version = 1;

    $hook_array['<event_name>'][] = array(
        <process_index>, //Integer
        '<description>', //String
        '<file_path>', //String or null if using namespaces
        '<class_name>', //String
        '<method_name>', //String
    );

钩方法

The hook action class can be located anywhere you choose. We recommended grouping the hooks with the module they are associated with in the 。/风俗/ directory. You can create a hook action method either with a namespace or without.

命名钩子

As of 7.7, developers can create namespaced logic hooks. When using namespaces, the file path in 。/风俗/ will be automatically built out when using the corresponding namespace. The filename defining the class must match the class name exactly to allow the autoloader to find the class definition.

命名空间 文件路径
Sugarcrm \ Sugarcrm \ Custom 。/风俗/
Sugarcrm \ Sugarcrm \ Custom 。/风俗/src/
Sugarcrm \ Sugarcrm \ Custom \ Any \ Path 。/风俗/any/path/
Sugarcrm \ Sugarcrm \ Custom \ Any \ Path 。/风俗/src/any/path/

。/风俗/extension/modules/accounts/ext/logichooks/<file>.php

<?php

    $hook_array['before_save'][] = array(
        1,
        'Hook description',
        null,
        'Sugarcrm\\Sugarcrm\\custom\\modules\\Accounts\\className',
        'methodName'
    );
?>

。/风俗/modules/accounts/classname.php.

<?php

namespace Sugarcrm \ Sugarcrm \ Custom\modules\Accounts;

    class className
    {
        function methodName($bean, $event, $arguments)
        {
            //logic
        }
    }
?>

逻辑挂钩方法签名可能包含不同的参数,具体取决于您选择的钩子事件。

没有名称空间的钩子

。/风俗/extension/modules/accounts/ext/logichooks/<file>.php

<?php

    $hook_array['before_save'][] = array(
        1,
        'Hook description',
        'custom/modules/Accounts/customLogicHook.php',
        'className',
        'methodName'
    );
?>

。/风俗/modules/accounts/customlogichook.php.

<?php

    class className
    {
        function methodName($bean, $event, $arguments)
        {
            //logic
        }
    }
?>

逻辑挂钩方法签名可能包含不同的参数,具体取决于您选择的钩子事件。

考虑

使用逻辑挂钩时,请记住以下最佳实践:

  • 至于PHP 5.3,对象通过引用自动传递。创建逻辑挂钩签名时,不附加&符(&) to the $bean variable. Doing this will cause unexpected behavior.
  • There is no hook that fires specifically for the ListView, DetailView or EditView events. Instead, use either the process_record or after_retrieve logic hooks.
  • In order to compare new values with previous values, use fetched_row to determine whether a change has occurred:
    if ($bean->fetched_row['{field}'] != $bean->{field})
    {
        //logic
    }
  • Make sure that the permissions on the logic_hooks.php 文件 and the class file that it references are readable by the web server. Otherwise, Sugar will not read the files and your business logic extensions will not work. For example, *nix developers who are extending the Tasks logic should use the following command for the logic_hooks 文件 and the same command for the class file that will be called:
    chmod +r ./custom/modules/Tasks/logic_hooks.php
  • Make sure that the entire 。/风俗/ directory is writable by the web server or else the logic hooks code will not work properly.

主题

    应用程序挂钩在使用全局应用程序时执行逻辑。
    使用糖模块(例如,帐户,案例等)时,模块挂钩执行逻辑。
    用户挂钩在用户操作周围执行逻辑,例如登录和注销。
    作业队列挂钩使用作业队列模块时执行逻辑。
    使用SNIP电子邮件归档服务时剪切钩子执行逻辑。
    使用REST API和路由时,API挂钩执行逻辑。
    Web逻辑挂钩让管理员在某些糖事件发生时将记录和事件信息发布到指定的URL。
    此页面将糖逻辑挂钩库中的历史更改记录。

最后修改:2020-06-17 13:03:50