Shane Dowling : SugarCRM — Add a code-driven column to a list

Sometimes you might find that you want your users to make decisions based on ListViews and you need to pull in relational data beyond the immediately related objects. Example

So using the circus example say you have performers who are performing for a specific show. You might want to see what other shows those performers are also attached to. If it's typically only one or two shows per event, you can probably make another column in the ListView that displays all shows related to that performer. Lets call this field 'Other Shows'.

What to do -—-— —

Add your new field in studio before you do anything. So fire up Studio and create an 'Other Shows' field, create this as a text field.

Alter the newly created fields vardef and set it to non-db.

You can typically find it in custom/Extension/modules/Performers/Ext/Vardefs/other_shows_c.php.

$dictionary['Performers']['fields']['other_shows_c'] = array(
 'name' => 'other_shows_c',
 'vname' => 'LBL_OTHER_SHOWS_C',
 'type' => 'varchar',
 'len' => '255',
 'source' => 'non-db'

Ensure the field isn't sortable..

Trying to sort a db/non-db field will cause a bit of trouble and ListViews don't support it, so just disable it.

Under custom/modules/Performers/metadata/listviewdefs.php change your newly created fields definition.

 array (
 'label' => 'Other Shows',
 'width' => '10%',
 'default' => true,
 'sortable' => false,

Adding the actual code to run.

For this we're going to leverage SugarCRM's logic hooks. If you haven't
already create a logic_hooks.php under custom/modules/Performers/ with
contents like this.

This file essentially defines the hooks you're going to call. The hook
process_record means it'll be called for every row that's displayed on
the ListView.

The performers_logic_hooks_class.php file will be included,
performers_logic_hooks is the class that it will look for and
list_record_process is the function within that class that it will
// Do not store anything in this file that is not part of the array or the hook version. This file will
 // be automatically rebuilt in the future.
 $hook_version = 1;
 $hook_array = Array();
 // position, file, function
 $hook_array['before_save'] = Array();
 $hook_array['before_save'][] = Array(1, 'workflow', 'include/workflow/WorkFlowHandler.php','WorkFlowHandler', 'WorkFlowHandler');
 $hook_array['process_record'][] = Array(1, 'Other_Shows', 'custom/modules/Performers/performers_logic_hooks.php','performers_logic_hooks', 'list_record_process');


Define the class.

Don't name the class something generic, like logic_hook_class and use that class name everywhere. It'll cause a bunch of conflicts when it comes to saving related data, hence the name performers_logic_hooks.

class performers_logic_hooks

 * Called as process_record logic hook on the Performers module
 public function list_record_process($bean, $event, $arguments)
 //Do some code to get the related shows for the performer and store in the $other_shows variable

$this->other_shows_c = $other_shows;


Add the field to your listview via studio and do a quick repair/rebuild. That should hopefully be it.