SugarCRM: Logic hook default handlers

This post is a little tip for all those SugarCRM developer noobies out there ( including myself ). When you want to do field validation serverside or do some business calculations while saving the bean(s) you should use logic hooks ( more info here ). Logic hooks can hook into several stages of the applications/module’s process.

Something that I have found very useful is to create a default class for handling all specific events and to copy this file to the instance of the project. Therefore I only need to worry about the customer specific things I have to do. A skeleton class for the module logic hooks could look like this:

<?php
class DefaultModuleLogicHookHandler{
	function before_delete($bean, $event, $arguments){}
	function after_delete($bean,$event,$arguments){}

	function before_relationship_add($bean, $event, $arguments){}
	function after_relationship_add($bean, $event, $arguments){}
	function after_relationship_delete($bean, $event, $arguments){}

	function after_restore($bean, $event, $arguments){}
	function after_retrieve($bean, $event, $arguments){}
	function after_save($bean, $event, $arguments){}

	function before_relationship_delete($bean, $event, $arguments){}
	function before_restore($bean, $event, $arguments){}
	function before_save($bean, $event, $arguments){}
	function handle_exception($bean, $event, $exception){}
	function process_record($bean, $event, $arguments){}

}
?>

Now when you start a new project the only thing you need to do is to make a copy of the file and add it to the correct directory . For instance /custom/modules/Accounts/AccountLogicHookHandler.php

and rename the class in that file to AccountModuleLogicHookHandler. In your logic_hooks.php you need to add the following to get it working for the  before_save

$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1,'AccountHandler','custom/modules/Accounts/AccountLogicHookHandler.php','AccountModuleLogicHookHandler','before_save');

And whenever you need to implement a new hook you only need to add the correct entry in the logic_hooks file and write the implementation. Easy as pie and saves a lot of time finding out which parameters each hook function has etc..

 

Happy coding!

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.