Sugar Community Edition 5.2 Documentation

Sugar Developer Guide

Table of Contents Previous Next

Version 5.2


Chapter 4 Customizing Sugar : Business Logic Hooks

Custom Logic (or "Business Logic Hooks") allows you to add functionality to certain actions, such as before a bean save, in an upgrade-safe manner. This is accomplished by defining hooks, which are placed in the custom/ directory, which will be called in the SugarBean in response to events at runtime. Because the code is located separate from the core SugarCRM code, it is upgrade-safe.
See this SugarForge project for a complete example of a useful business hook.
The code that declares your custom logic is located in: custom/modules/<CURRENT_MODULE>/logic_hooks.php. The format of the declaration follows.
All logic hooks should define the $hook_version that should be used. Currently, the only supported $hook_version is 1.
Your logic hook will also define the $hook_array. $hook_array is a two dimensional array:
o
name: the name of the event that you are hooking your custom logic to
o
array: an array containing the parameters needed to fire the hook
o
$bean - $this bean passed in by reference.
o
$event - The string for the current event (i.e. before_save)
o
$arguments - An array of arguments that are specific to the event.
The check_logic_hook_file() command is looking for three things. First is the module that you want this hook to affect, in this case "Accounts". The second is the hook itself, in this case "after_retrieve". The third part is an array which mirrors the $hook_array like this:
Of course you can call the check_logic_hook_file() command as many times as you want. The logic behind the command check to make sure that your hook isn't already set and if it is then it replaces it, making it pretty upgrade-safe. If you have any questions about installing logic hooks look for me in the forums.
Above we showed how to create a custom_logic_hook that runs the function updateDescription() from the class updateDescription (those don't have to be the same, they just are in this example) in the PHP file updateDescription.php. Below is the actual script from that PHP file.
You see that the $bean is fed into the function and allows access to all of the fields for the currently selected record. Any changes you make to the array will be reflected in the actual data. For example in this script we are changing the description field. As you see above there is
$event is set to whatever event is currently running like after_retrieve or before_delete.
o
In order to compare new values with previous values to see whether a change has happened, use $bean->fetched_row['<field>'] (old value) and $bean-><field> (new value) in the before_save logic hook.
o
Make sure that the permissions on your logic_hooks.php file and the class file that it references are readable by the Web server. If this isn’t done, the files will not be read by the SugarCRM application and your business logic extensions will not work.
For example, for *nix developers who are extending the Tasks logic, that means you should do the following command for the logic_hooks file and the same command for the class file that will be called.
o
Make sure that the entire custom/ directory is writable by the Web server, otherwise the logic hooks code will not work properly.

Table of Contents Previous Next

Copyright 2004-2009 SugarCRM Inc.
Product License

User Contributed Comments

No user comments found.

Add a Comment
E-mail:
Comment:
Have feedback for us? Drop us a line.
Terms & Conditions | Privacy | Trademark Info | Contact Info | FAQs | SugarCRM Inc.© 2004 - 2009 All rights reserved.