Subpanel Buttons

sugarcrmdevelopers —  January 11, 2012

By default, it looks like any new many:many or one:many relationship created in Studio is given 1) a subpanel and 2) a set of Create and Select buttons for said subpanel. But if you look around the system, you’ll see several instances where subpanels have only one or the other, or neither, or even several other buttons (e.g. Activities). Let’s break down the subpanel definitions that define the related module, or modules, how these buttons are defined, and how we can customize them in an upgrade-safe way. In this example, we’ll walk through removing the Create button from the Contacts Subpanel on the Accounts Detailview.

Original subpanel & subpanel-button definitions are held in the modules’ metadata directories, in the subpaneldefs file (e.g. modules/Accounts/metadata/subpaneldefs.php). In this file, we see the module’s subpanel_setup array, containing each and every [default] subpanel arrangement, including labels, related module data, and button layout (top_buttons). Of course, modifying this file won’t be upgrade-safe, so we need to get into the custom Extension directory to write some code there.

Head to custom/Extension/modules/Accounts/Ext/Layoutdefs. If you’ve deployed any custom relationships that would involve a subpanel (many:many or one:many) then you may already see some files here. If not, create the file path and create a new file. Any filename will do, let’s just go with custom/Extension/modules/Accounts/Ext/Layoutdefs/Account_Contact_Hide_Create_Button.php. We will want to redefine as little as possible in the array, so this should suffice:

<?php
$layout_defs["Accounts"]["subpanel_setup"]["contacts"]["top_buttons"] = array(
    array('widget_class' => 'SubPanelTopSelectButton', 'mode'=>'MultiSelect'),
);

Notice how this differs from the original definition below, by simply redefining top_buttons in the same way, but dropping the first key on the Array that refers to the Create button:

<?php
$layout_defs["Accounts"]["subpanel_setup"]["contacts"] = array(
	'order' => 30,
	'module' => 'Contacts',
	'sort_order' => 'asc',
	'sort_by' => 'last_name, first_name',
	'subpanel_name' => 'ForAccounts',
	'get_subpanel_data' => 'contacts',
	'add_subpanel_data' => 'contact_id',
	'title_key' => 'LBL_CONTACTS_SUBPANEL_TITLE',
	'top_buttons' => array(
		array('widget_class' => 'SubPanelTopCreateAccountNameButton'),
		array('widget_class' => 'SubPanelTopSelectButton', 'mode'=>'MultiSelect')
	),
),

With this file in place, simply run a Repair and Rebuild and your code will be copied from the Extension directory into the working, in-effect directory custom/modules/Accounts/Ext/Layoutdefs/layoutdefs.ext.php. From that point, your changes should be visible on-screen. Everything will be the same, but simply lacking the undesired Create button.

Now that you’re a pro, you might as well have a look at the next step, creating New Subpanel Buttons.