A previous post covered adding a button to the List View. However this was adding an action button to the list as a whole. This post will cover adding buttons for each row.

By the end of this post you will be able to add buttons such as this one:

ViewOnCalButton

Our example will look at the calls module. We’ll add a simple button to view the call on the calendar.

First off we will create a new non-db field in the calls module. We add the following file to custom/Extension/modules/Calls/Ext/Vardefs/ViewOnCalButton.php (we use the filename ‘ViewOnCalButton.php’ but this can be whatever you wish).

<?php
$dictionary['Call']['fields']['view_on_cal_button'] = array(
    'name' => 'view_on_cal_button',
    'vname' => 'LBL_VIEW_ON_CAL_BUTTON',
    'type' => 'varchar',
    'source' => 'non-db',
);

Next we add the language string for our new field. We place this file in custom/Extension/modules/Calls/Ext/Language/en_us.ViewOnCalButton.php:

<?php
$mod_strings['LBL_VIEW_ON_CAL_BUTTON'] = 'View on Calendar';

In order to actually have the field display as a button we set up a logic hook to fill in the value. First we must define the logic hook in custom/Extension/modules/Calls/Ext/LogicHooks/ViewOnCal.php:

<?php
if(empty($hook_array)){
    $hook_array = array();
}
if(empty($hook_array['process_record'])) {
    $hook_array['process_record'] = array();
}
$hook_array['process_record'][] = Array(1, 'Add View On Cal Button', 'custom/modules/Calls/ViewOnCalHook.php', 'ViewOnCalHook', 'addButton');

And then implement the hook itself and add it to custom/modules/Calls/ViewOnCalHook.php:

<?php
class ViewOnCalHook{
    function addButton($focus, $event, $args){
        global $timedate;
        $date = $timedate->fromUser($focus->date_start);
        $year = $date->format('Y');
        $month = $date->format('m');
        $day = $date->format('d');
        $loc = "index.php?action=index&module=Calendar&view=week&&year={$year}&month={$month}&day={$day}&hour=0";
        $focus->view_on_cal_button = "<button onclick='document.location = \"{$loc}\"' type='button'>View on calendar</button>";
    }
}

Note that we are creating a button which links to the calendar but this can do whatever actions you require. We must do a quick repair and rebuild from the admin menu in order for our changes above to take effect.

Finally we add the button to the list view. This can either be done in studio or by editing the list view defs. Let’s do this through studio:

ViewCalButton
Studio

And there we have it. We now have the following:

ViewOnCalButton

Upon clicking we should be taken to the calendar.

Note that the above button is just an example and has a few issues that could be improved upon. If we click “View on calendar” for a call that isn’t ours we will be taken to a calendar page without the call on it. Can you see any other improvements? Let me know in the comments,