For those of you who haven’t had the chance to look into SugarCRM logic hooks, they’re a handy little tool for customizing your instance of this powerful system. You can use them to manipulate fields, such as performing mathematical operations on fields or pre-populating fields based on other database values.
But one of the tragic flaws of their logic hook implementation is that there is no hook when the Edit View fires to create a new record. A hook does exist for when you save a new record, but not in the case that, for instance, you’d want to populate a dropdown list from the database when you create a new record. The following little hack will help you call the “After Retrieve” logic hook when you are creating a new record.
First, you need to create a custom logic_hooks.php file in an upgrade-safe directory for your module. If you already have a logic_hooks.php file, then you can just infer from the example below and modify your existing file. If you do not have a logic_hooks.php file, create one and put it in /custom/modules/<module_name>. It should look something like this:
<?phpif (!defined(‘sugarEntry’) || !sugarEntry) die(‘Not A Valid Entry Point’);// Set the version hooks should use, currently Sugar only supports version 1.$hook_version = 1;// Initialize the hook array$hook_array = Array();// Initialize the hook type$hook_array['after_retrieve'] = Array();$hook_array['after_retrieve'][] = array(1, //Hook version‘Do stuff before creating some record’, //Label‘custom/modules/<module_name>/SomeClassName.php’, //Include file‘SomeClassName’, //Class‘SomeMethodName’ //Method);?>
Now, create the file with the class and method that you want to call when you open the edit view to create a new record. Here’s an example to set the values of a dropdown list. You can do a lot of stuff here, but this seems to be a common enough need to include for an example.
<?phpclass SomeClassName{function SomeMethodName(&$bean, $event, $arguments) {//sugar globalsglobal $app_list_strings;// query the db for CAMPAIGNS$query = ‘SELECT name FROM your_module_table WHERE deleted = 0?;$resp = $bean->db->query($query);$app_list_strings['your_dropdown_list'][] = “”;// set app_list_stringswhile ($row = $bean->db->fetchByAssoc($resp)){$app_list_strings['your_dropdown_list'][$row['name']] = $row['name'];}} // SomeMethodName} // SomeClassName?>
None of the above is particularly enlightening, and you can find most of it in the SugarCRM developer guide. But the following took me about two weeks of packing to find. This is the really special bit that will add your logic hook to the edit view–even when it is being called on for a new record.
Create a custom view.edit.php in the “Views” folder for your module. If you’re new to Sugar, you should be aware that you can create these files and folders when they don’t exist. It doesn’t work for every custom file you want to make, but if you’re curious you should, again, read the manual.
<?phprequire_once(‘include/MVC/View/views/view.edit.php’);class my_moduleViewEdit extends ViewEdit{function my_moduleViewEdit(){parent::SugarView();}function predisplay(){global $mod_strings;parent::predisplay();if (!$this->bean->id) $this->bean->call_custom_logic(‘after_retrieve’);}}?>
That’s all there is to it! This custom edit view code will trigger the after_retrieve logic hook whenever you call the edit view to create a new record. You can substitute any of the other logic hooks here, too, of course.
{ SugarCRM }
