Tuesday, September 06, 2011

Making Sugar fields sortable and linkable

I am using Sugar Community Edition, Version 6.2.0RC3 (Build 6350).

I found that in the list views for custom modules in Sugar - and in the sub-panels Sugar creates when you make relationships between custom modules in Sugar - only name fields appear as links to the items being listed. Furthermore, it only allows name fields to be sortable by default. This is annoying default behaviour - particularly if you don't want to use a name field. Thankfully, it is relatively easy to fix.

Linking requires a different fix for list views and sub-panels. I came across a post in the Sugar forums that gave me the clues I needed for this: No list view link.

Linking in list views. You need to find the listviewdefs.php file for the particular module you are working on. This path will differ slightly depending on whether you have the module in a package or not (I do). In my example, assume I have a field called "FIELD_P" in a module called "Module_X" in a package called "Package_A". Look for the file <SUGAR_INSTANCE>\custom\modulebuilder\packages\Package_A\modules\Module_X\metadata\listviewdefs.php. (The path to SUGAR_INSTANCE will be a the unzipped Sugar install deployed to an Apache instance.) Within that file, find the array representing the field you want to make "linkable". It should look like this:

'FIELD_P' =>
   array (
      'type' => 'enum',
      'studio' => 'visible',
      'label' => 'LBL_FIELD_P',
      'sortable' => false,
      'width' => '10%',
      'default' => true,
   ),

All you need to do is add another array element, 'link' => true, like so:

'FIELD_P' =>
   array (
      'type' => 'enum',
      'studio' => 'visible',
      'label' => 'LBL_FIELD_P',
      'sortable' => false,
      'width' => '10%',
      'default' => true,
      'link' => true,
   ),

Linking in sub-panels. Find the PHP file for the subpanel. For my example, the default sub-panel will be in this file: <SUGAR_INSTANCE>\custom\modulebuilder\packages\Package_A\modules\Module_X\metadata\subpanels\default.php. Again, find the array representing the field you want to make "linkable". It should look like this:

'FIELD_P' =>
   array (
      'type' => 'enum',
      'studio' => 'visible',
      'label' => 'LBL_FIELD_P',
      'sortable' => false,
      'width' => '10%',
      'default' => true,
   ),

All you need to do is add another array element, 'widget_class' => 'SubPanelDetailViewLink', like so:

'FIELD_P' =>
   array (
      'type' => 'enum',
      'studio' => 'visible',
      'label' => 'LBL_FIELD_P',
      'sortable' => false,
      'width' => '10%',
      'default' => true,
      'widget_class' => 'SubPanelDetailViewLink',
   ),

Sorting is even easier, and you have already seen where to fix it for both sub-panels and list views - change 'sortable' => false, in the above two examples to 'sortable' => true,.

Final note: you are making changes to files that Sugar has generated. I noticed that whenever I requested further deployments and cache refreshes, the changes remained - but who can tell what sort of action will blow those changes away. For this reason, it is very important to back up your Sugar files regularly.