Let the platform do the work

LogicHooks

Overview

The LogicHooks extension adds actions to specific events such as, for example, before saving a bean. For more information on logic hooks in Sugar, please refer to the Logic Hooks documentation.

Properties

The following extension properties are available. For more information, please refer to the Extension Property documentation.

Property Value
Extension Scope All - Application & Module
Sugar Variable $hook_array
Extension Directory

Application - ./custom/Extension/application/Ext/LogicHooks/

Module - ./custom/Extension/modules/<module>/Ext/LogicHooks/

Compiled Extension File

Application - ./custom/application/Ext/LogicHooks/logichooks.ext.php

Module - ./custom/modules/<module>/Ext/LogicHooks/logichooks.ext.php

Manifest Installdef $installdefs['hookdefs']

Implementation

The following sections illustrate the various ways to implement a customization to a Sugar instance.

File System

When working directly with the filesystem, you can create a file in ./custom/Extension/application/Ext/LogicHooks/ to add a new logic hook to the application. The following example will create a new before_save logic hook that executes for all modules:

./custom/Extension/application/Ext/LogicHooks/<file>.php

  <?php

    $hook_array['before_save'][] = Array(
        1, 
        'Custom Logic', 
        'custom/application_hook.php', 
        'ApplicationHookConsumer', 
        'before_method'
    );

Next, create the hook class:

./custom/application_hook.php

  <?php

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

    class ApplicationHookConsumer
    {
        function before_method($bean, $event, $arguments)
        {
            //logic
        }
    }

?>

Finally, navigate to Admin > Repair > Quick Repair and Rebuild. The system will then rebuild the extensions and the customizations will be compiled into ./custom/application/Ext/LogicHooks/logichooks.ext.php. Your logic hook will run before saving records in any module.

Module Loadable Package

When building a module loadable package, you can use the $installdefs['hookdefs'] index to install the extension file.

Installdef Properties

Name Type Description
from String The basepath of the file to be installed.
to_module String The key of the module the file is to be installed to.

The example below demonstrates the proper install definition that should be used in the ./manifest.php file in order to add the Action View Map file to a specific module. You should note that when using this approach, you still need to use the $installdefs['copy'] index for the hook class file, however Sugar will automatically execute Rebuild Extensions to reflect the new logic hook in the system.

./manifest.php

  <?php

$manifest = array(
    ...
);

$installdefs = array(
    'id' => 'actionView_example',
    'hookdefs' => array(
        array(
            'from' => '<basepath>/Files/custom/Extension/application/Ext/LogicHooks/<file>.php',
            'to_module' => 'application',
        )
    ),
    'copy' => array(
        array(
            'from' => '<basepath>/Files/custom/application_hook.php',
            'to' => 'custom/application_hook.php',
        ),
    )
);

Alternatively, you may use the $installdefs['copy'] index to copy the file. When using this approach, you may need to manually run repair actions such as a Quick Repair and Rebuild. For more information on the $installdefs['copy'] index and module-loadable packages, please refer to the Introduction to the Manifest page.

Alternative Installdef

Although not recommended, you could utilize the $installdefs['logic_hooks'] index to deploy a logic hook to the system. Please note that there are a couple caveats to this installation method:

  • The $installdefs['logic_hooks'] index method only works for module-based logic hooks.
  • The $installdefs['logic_hooks'] index method installs to ./custom/modules/<module>/logic_hooks.php, which is not part of the Extension framework.

Properties

Name Type Description
module String The key of the module for the logic hook to be installed to.
hook String The type of logic hook to be installed.
order Integer The number in which the logic hook should run.
description String A description of the logic hook to be installed.
file String The file path which contains the logic hook class.
class String The class which houses the logic hook functionality.
function String The function the logic hook will execute.

The example below will demonstrate the $installdefs['logic_hooks'] index in the ./manifest.php file, in order to add an after save logic hook to a specific module. You should note that when using this approach, you still need to use the $installdefs['copy'] index for the hook class file.

./manifest.php

  <?php

$manifest = array(
    ...
);

$installdefs = array(
    'id' => 'actionView_example',
    'logic_hooks' => array(
        array(
            'module' => '<module>',
            'hook' => 'after_save',
            'order' => 1,
            'description' => 'Example After Save LogicHook',
            'file' => 'custom/modules/<module>/module_hook.php',
            'class' => '<module>HookConsumer'
            'function' => 'after'
        )
    ),
    'copy' => array(
        array(
            'from' => '<basepath>/Files/custom/modules/<module>/module_hook.php',
            'to' => 'custom/modules/<module>/module_hook.php',
        ),
    )
);

Topics