After digging around some SugarCRM code, I ran across an interesting jQuery plugin in /includes/javascript/jquery/jquery.SugarMenu.js. After exploring where this plugin is used throughout the codebase, I learned that it actually powers all of those cool buttons you see on all of the views throughout SugarCRM: above and below list views, above detail views, in every subpanel, etc. Another interesting thing in the plugin is that it is well-documented, thanks to John Barlow and Justin Park at SugarCRM whenever they wrote the plugin! Because of this, I was able to easily see the handy addItem() method that comes with the plugin.
So... on to the good stuff: the easiest way to add items to buttons throughout SugarCRM. You simply use jQuery to select the parent <ul> and tell the plugin to add an item:
$('#selector').sugarActionMenu('addItem',{ | |
item: $('<li><a href="#">New Item!</a></li>'), // a jquery element to add, | |
index: 1 // (optional) set the exact order the new option will show in the list (1 being the first item) | |
}); |
The great thing about this and the modularization of SugarCRM is that the underlying markup for the standard views is pretty much the same for all modules: a subpanel is subpanel, a list view is a list view, etc. Because of this, we can reliably use jQuery selectors to select and modify any button that uses this plugin. In all of the base view tpls (such as /includes/ListView/ListViewGeneric.tpl) you'll see that for every page in SugarCRM any unordered list elements with the class clickMenu will be transformed to a sugarActionMenu via this plugin.
For all of the most common action menus throughout SugarCRM, I've created a file of example javascript that it will take to add a menu item:
Now that we have the required javascript needed to add items to the various buttons, we need a reliable way to add the javascript into the pages we have menu buttons we want to modify. The best way I've found to do this is by using the after_ui_frame logic hook. To do that, we simply add an entry to a specific module's or global logic hook. I use the Accounts module for the following examples.
<?php | |
$hook_version = 1; | |
$hook_array = array(); | |
$hook_array['after_ui_frame'] = array(); | |
$hook_array['after_ui_frame'][] = Array(1, 'Accounts InsideView frame', 'modules/Connectors/connectors/sources/ext/rest/insideview/InsideViewLogicHook.php','InsideViewLogicHook', 'showFrame'); | |
// Add the new hook | |
$hook_array['after_ui_frame'][] = Array(2, 'Add Buttons to Account Module Views', 'custom/modules/Accounts/AccountButtons.php','AccountButtons', 'add'); |
Now we need to create the php script that executes for when this logic hook is triggered. I've called the class AccountButtons and the method add(). The after_ui_frame logic hook will fire for all views for the module you're using, so you need to be sure to only add your relevant javascript to the views that you want. In the code below, I add several buttons to the detail view and list view. To get the javascript to execute in the specific views, you simply need to echo the javascript code.
And that's it!
If you're wanting to dig deeper into the code, you may find it easier to digest on Github
-
CleverReach Integration for Sugar
FEATUREDThe CleverReach Integration for Sugar solves major challenges for marketing employees by reducing many tedious manual processes steps. The add-on enhances collaboration between sales and marketing an... -
Scoring / Rating field module
**It has never been easier to Score Your Data** Add rating fields to any SugarCRM module right from within Studio. No coding required ! Choose the maximum number of stars to display, allow (or not) ha... -
E-Mail Signature Manager Module
People are constantly sending emails. Add this to your install to make that process faster by adding signature templates. This add-on allows administrators to streamline E-Mail signature management an... - Show more addons