Sugarcrm支持产品指南糖开发人员糖开发人员指南10.3食谱模块可加载软件包为逻辑挂钩创建可安装包
此版本仅适用于Sugarcloud客户。

为逻辑挂钩创建可安装包

概述

这些示例将演示如何基于不同方案创建逻辑挂钩的模块可加载软件包。

逻辑挂钩文件与逻辑挂钩定义

No matter the deployment method, all logic hooks require being registered (meaning they are attached to the $hook_array array), defining the logic hook event, module, file to load, and class and function to run for that logic hook event. For all deployment options, the logic hook definition points to a file where the class and function to run is located, and this file must be copied to the instance via the copy installdefs of the manifest. The bulk of this article will focus on the various options for registering the logic hook definition via the package manifest. But all deployment strategies will assume the same logic hook definition, the same logic hook file, and will use the same copy array to move the logic hook file into the instance.

笔记: More information on the $manifest or $installdef can be found in the 介绍清单 .

逻辑钩

此逻辑挂钩将将PROFE_SAVE挂钩安装到帐户模块,该帐户模块将默认帐户名称为“我的新帐户名称({time Stump})”。这种安装此挂钩的各种方式将显示在以下部分中。

逻辑挂钩定义

<basepath>/files/custom/extension/accounts/ext/logichooks/account_name_hook.php.

<?php

$hook_array['before_save'][] = Array(
    99, 
    'Example Logic Hook - Updates account name', 
    'custom/modules/Accounts/accounts_save.php', 
    'Accounts_Save', 
    'updateAccountName'
);

逻辑挂钩文件

<basepath>/files/custom/modules/accounts/accounts_save.php.

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class Accounts_Save
{
	function updateAccountName($bean, $event, $arguments)
	{
		$bean->name = "My New Account Name (" . time() . ")";
	}
}

分布式插件

如果构建的包装旨在将各种糖实例分发为分布式插件的一部分,则应将您的逻辑挂钩定义作为分机框架的一部分使用 Logicooks Extension., which can be done using $installdefs['logic_hooks'] in the package manifest. When using this extension, the logic hook will be installed and uninstalled along with the plugin.

笔记: More information on the $installdefs['logic_hooks'] can be found in Logichooks扩展名.

包例示例

The following files are relative to the root of the .zip archive which is referred to at the basepath. This example package can be downloaded 这里.

<basepath>/manifest.php

<?php

$manifest = array(
    'acceptable_sugar_flavors' => array(
        'PRO',
        'ENT',
        'ULT'
    ),
    'acceptable_sugar_versions' => array(
        'exact_matches' => array(),
        'regex_matches' => array(
            '10.3.*
        '),
    ),
    'author' => 'SugarCRM',
    'description' => 'Installs a sample logic hook - using extension framework',
    'icon' => '',
    'is_uninstallable' => true,
    'name' => 'Example Logic Hook Installer - Using Extension Framework',
    'published_date' => '2018-01-01 00:00:00',
    'type' => 'module',
    'version' => '1.0',
);

$installdefs = array(
	'id' => 'package_123',
	'copy' => array(
		array(
			'from' => '<basepath>/files/custom/modules/accounts/accounts_save.php.',
			'to' => 'custom/modules/Accounts/accounts_save.php',
		),
	),
	'hookdefs' => array(
		array(
			'from' => '<basepath>/files/custom/extension/accounts/ext/logichooks/account_name_hook.php.',
			'to_module' => 'Accounts',
		)
	),
);

<basepath>/files/custom/extension/accounts/ext/logichooks/account_name_hook.php.

<?php

$hook_array['before_save'][] = Array(
    99, 
    'Example Logic Hook - Updates account name', 
    'custom/modules/Accounts/accounts_save.php', 
    'Accounts_Save', 
    'updateAccountName'
);

<basepath>/files/custom/modules/accounts/accounts_save.php.

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class Accounts_Save
{
	function updateAccountName($bean, $event, $arguments)
	{
		$bean->name = "My New Account Name (" . time() . ")";
	}
}

促销过程

如果构建的包装旨在成为代码促销过程的一部分,欢迎您使用 Logicooks Extension. as mentioned above or you may find it easier to update the ./custom/modules/<module>/logic_hooks.php file, which can be done using the $installdefs['logic_hooks'] in the package manifest.

包例示例

The following files are relative to the root of the .zip archive which is referred to at the basepath. This example package can be downloaded 这里.

<basepath>/manifest.php

<?php

$manifest = array(
        'acceptable_sugar_flavors' => array(
            'PRO',
            'ENT',
            'ULT'
        ),
        'acceptable_sugar_versions' => array(
            'exact_matches' => array(),
            'regex_matches' => array(
                '10.3.*
            '),
        ),
	'author' => 'SugarCRM',
	'description' => 'Installs a sample logic hook - using logic_hooks installdefs',
	'icon' => '',
	'is_uninstallable' => true,
	'name' => 'Example Logic Hook Installer - Using logic_hooks installdefs',
	'published_date' => '2018-01-01 00:00:00',
	'type' => 'module',
	'version' => '1.0',
);

$installdefs = array(
	'id' => 'package_456',
	'copy' => array(
		array(
			'from' => '<basepath>/files/custom/modules/accounts/accounts_save.php.',
			'to' => 'custom/modules/Accounts/accounts_save.php',
		),
	),
	'logic_hooks' => array(
		array(
			'module' => 'Accounts',
			'hook' => 'before_save',
			'order' => 99,
			'description' => 'Example Logic Hook - Updates account name',
			'file' => 'custom/modules/Accounts/accounts_save.php',
			'class' => 'Accounts_Save',
			'function' => 'updateAccountName',
		),
	),
);

<basepath>/files/custom/modules/accounts/accounts_save.php.

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class Accounts_Save
{
	function updateAccountName($bean, $event, $arguments)
	{
		$bean->name = "My New Account Name (" . time() . ")";
	}
}

有条件地通过post_execute脚本添加或删除逻辑钩

A developer may want to be able to trigger the addition or removal of logic hooks from a customization. This can be done using a post execute script with the check_logic_hook_file and remove_logic_hook_file functions.

在执行此操作时建议谨慎态度,并将任何更新限制为Off-Mounds,而不是影响用户的系统行为。

笔记remove_logic_hook_file only removes hooks defined in the given module's logic_hooks.php file. check_logic_hook_file only checks hooks defined in the given module's logic_hooks.php file. Neither check or remove hooks defined via the Logic Hook Extension framework. Thus this deployment option should be considered a subset of the Promotion Process deployment option listed earlier.

包例示例

笔记: For demonstration purposes, in the post_execute script, the Accounts_Save logic hook from the earlier example package is unregistered (if it exists) and a second logic hook Accounts_Save_Enterprise is registered. The second logic hook points to a different logic hook file, which is moved to the instance using copy in the manifest.

The following files are relative to the root of the .zip archive which is referred to at the basepath. This example package can be downloaded 这里.

<basepath>/manifest.php

<?php

$manifest = array(
        'acceptable_sugar_flavors' => array(
            'PRO',
            'ENT',
            'ULT'
        ),
        'acceptable_sugar_versions' => array(
            'exact_matches' => array(),
            'regex_matches' => array(
                '10.3.*
            '),
        ),
	'author' => 'SugarCRM',
	'description' => 'Installs a sample logic hook - using post_execute check_logic_hook_file',
	'icon' => '',
	'is_uninstallable' => true,
	'name' => 'Example Logic Hook Installer - Using post_execute check_logic_hook_file',
	'published_date' => '2018-01-01 00:00:00',
	'type' => 'module',
	'version' => '1.0',
);

$installdefs = array(
	'id' => 'package_789',
	'copy' => array(
		array(
			'from' => '<basepath>/files/custom/modules/accounts/accounts_save_enterprise.php.',
			'to' => 'custom/modules/Accounts/accounts_save_enterprise.php',
		),
	),
	'post_execute' => array(
		 '<basepath>/change_hooks.php',
	),

);

<basepath>/files/custom/modules/accounts/accounts_save_enterprise.php.

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class Accounts_Save_Enterprise
{
	function updateAccountName($bean, $event, $arguments)
	{
		$bean->name = "My New Account Name (" . time() . ") -- Enterprise";
	}
}

<basepath>/change_hooks.php

<?php

if(!defined('sugarEntry')) define('sugarEntry', true);

require_once 'include/entryPoint.php';

$accounts_hook_pro = array(
	99,
	'Example Logic Hook - Updates account name',
	'custom/modules/Accounts/accounts_save.php',
	'Accounts_Save',
	'updateAccountName'
);

$accounts_hook_ent = array(
	99,
	'Example Logic Hook - Updates account name - Enterprise',
	'custom/modules/Accounts/accounts_save_enterprise.php',
	'Accounts_Save_Enterprise',
	'updateAccountName'
);

if( $GLOBALS['sugar_flavor'] == 'ENT' ) {

	//unregister Accounts_Save logichook
	remove_logic_hook("Accounts", "before_save", $accounts_hook_pro);

	//register Accounts_Save_Enterprise logichook
	check_logic_hook_file("Accounts", "before_save", $accounts_hook_ent);
}

最后修改:2021-01-06 23:28:41