SugarCRM Logic Hook Improvements

Edit 1: In response to comments from Jason Eggers I have updated my closure concept to include labels so that they can be identified and uninstalled.

Edit 2: Chad Hutchins further contributed to this concept with a basic outline of a helper class. This post has been updated accordingly.

Like a lot of you SugarCRM dev’s, I find myself creating simple logic hooks fairly often. While this is a simple task I feel that it could be even simpler. Lets look at the current process of creating a logic hook.

1. Create the file /custom/modules/{module}/logic_hooks.php

2. Create your logic hook class at /custom/modules/{module}/logic_hooks_class.php

Two easy steps. That’s all it takes. While this is pretty straight forward I cant help but feel like its an unnecessary amount of work for executing 1 function.

If logic_hooks.php were to support closures (PHP 5.3.0 +) we could achieve all of the above in 1 file (logic_hooks.php) and eliminate the need for the “logic_hooks_class” class all together.

Here is a brief example of how it could potentially work:

In 1 file and roughly 10% of the initial code required we have included all of our logic which makes the logic hook easier to maintain and quicker to write. Personally, this would eliminate at least 75% of my logic hook related files.

Somewhere deeper in the SugarCRM platform this code could be executed with something along the following lines:

To further make using logic hooks more efficient Chad Hutchins has contributed the following helper class concept to make adding and removing logic hooks that much easier.

Thoughts? Opinions? I would love to hear some feedback on this concept!