As many of you already know, logic hooks allow you to insert business logic (php code) at certain points within common actions a SugarCRM user may perform. Commonly used logic hooks are before_save and after_save hooks which allow you to run code after or before a record is saved, and after_login and after_logout which allows you to run code after a user logs in or out. The (mostly) complete list of available hooks and how to use them are available on the SugarCRM Developer website.

In some cases, you may need a single logic hook to work for all modules. Global logic hooks are good to use when you’re generalizing a utility for all modules. Instead of having an individual logic_hooks.php file in every /custom/modules/{module} folder, you can create /custom/modules/logic_hooks.php and put your hooks in there. The hooks in the global logic hooks file will fire for every module, even custom modules.

One issue I ran into recently was how to package and distribute a global logic hook. Installing a logic hook with a “Module Loader”-loadable package is pretty easy. Angel Magaña has a post explaining how to do just that. Adding a global logic hook is very simple. You do exactly what you would to install a regular logic hook using the logichooks entry in the $installdefs array, and instead of providing a module name, you provide an empty string.

<?php
$installdefs = array(
'id' => 'Global_Logic_Hook_Testing',
'copy' => array(
// move file that contains the class and
// method for the logic hooks below
array(
'from' => '<basepath>/accounts_logic_hook.php',
'to' => 'custom/modules/Accounts/accounts_logic_hook.php',
),
array(
'from' => '<basepath>/global_logic_hook.php',
'to' => 'custom/modules/global_logic_hook.php',
),
),
'logic_hooks' => array(
// regular logic hook for Accounts module
array(
'module' => 'Accounts',
'hook' => 'before_save',
'order' => 50,
'description' => 'Do work on the Accounts module SON!',
'file' => 'custom/modules/Accounts/accounts_logic_hook.php',
'class' => 'Accounts_logic_hook_class',
'function' => 'Accounts_logic_hook_method',
),
// global logic hook
array(
'module' => '',
'hook' => 'before_save',
'order' => 60,
'description' => 'Do global work SON!',
'file' => 'custom/modules/Contacts/test.php',
'class' => 'Global_logic_hook_class',
'function' => 'Global_logic_hook_method',
),
),
);
$manifest = array(
'acceptable_sugar_versions' => array('regex_matches' => array(0 => '6\.*'),),
'acceptable_sugar_flavors' => array(0 => 'CE',1 => 'PRO',2 => 'ENT',),
'name' => 'Global Logic Hook',
'description' => 'Showing how to install a global logic hook',
'is_uninstallable' => true,
'author' => 'Chad Hutchins, SugarOutfitters',
'published_date' => 'November 28, 2012',
'version' => '1.0.3',
'type' => 'module',
);
view raw manifest.php hosted with ❤ by GitHub
<?php
class Accounts_logic_hook_class
{
function Accounts_logic_hook_method(&$bean, $event, $arguments)
{
echo "Hit the Accounts before save logic hook";
exit;
}
}
<?php
class Global_logic_hook_class
{
function Global_logic_hook_method(&$bean, $event, $arguments)
{
echo "Hit the global before save logic hook";
exit;
}
}

To test this out for yourself you could use git to checkout the code or manually download the files from github. Zip up the three files together in a .zip file and load it up using the module loader. After you install the package, you’ll see the logic_hooks.php files were created properly in both /custom/modules and custom/modules/Accounts.

What other useful ways have you found to install and implement logic hooks?