Convert the country text field in SugarCRM CE or SuiteCRM to a drop-down list

If you need to change the country text field into a drop-down list in any of the sugarCRM’s modules this post will show you how.

Step 1 Create the vardef for your country field

custom/Extension/modules/[THE_MODULE_NAME]/Ext/Vardefs/custom_primary_address_country.php

$dictionary['<MODULE_NAME>']['fields']['primary_address_country']['comments']='Country for primary address';
$dictionary['<MODULE_NAME>']['fields']['primary_address_country']['group']='primary_address';
$dictionary['<MODULE_NAME>']['fields']['primary_address_country']['options']='countries_dom';
$dictionary['<MODULE_NAME>']['fields']['primary_address_country']['type']='enum';

Step 2 Make a copy of the edit view template for the Address fields

Create a new directory here:
/custom/include/SugarFields/Fields/[CUSTOM_FIELD_TYPE_NAME]/EditView.tpl

Then copy the EditView template from the lcation below into the new directory
/include/SugarFields/Fields/Address/EditView.tpl

Step 3 Edit the copy of template in your new directory

Find this line:

<input type="text" name="{{$country}}" id="{{$country}}" size="{{$displayParams.size|default:30}}" {{if !empty($vardef.len)}}maxlength='{{$vardef.len}}'{{/if}} value='{$fields.{{$country}}.value}' tabindex="{{$tabindex}}">

and change it to the following:

<select name="{{$country}}" width="{{$displayParams.size|default:30}}" id="{{$country}}" title="{{$vardef.help}}" tabindex="{{$tabindex}}" {{if isset($displayParams.script)}}{{$displayParams.script}}{{/if}}>
{if isset($fields.{{$country}}.value) && $fields.{{$country}}.value != ''}
 {html_options options=$fields.{{$country}}.options selected=$fields.{{$country}}.value}
{else}
 {html_options options=$fields.{{$country}}.options selected=$fields.{{$country}}.default_value}
{/if}
</select>

Step 4 Change the EditView defs to display your new field type

In /custom/modules/[THE_MODULE_NAME]/metadata/editviewdefs.php change “type” to your new custom field type name.

 0 =>
      array (
        'name' => 'primary_address_street',
        'hideLabel' => true,
        'type' => '<CUSTOM_TYPE_NAME>',
        'displayParams' =>
        array (
          'key' => 'primary',
          'rows' => 2,
          'cols' => 30,
          'maxlength' => 150,
        ),
        'label' => 'LBL_PRIMARY_ADDRESS_STREET',
      ),

If /custom/modules/[THE_MODULE_NAME]/metadata/editviewdefs.php does not exist just make a change to the editview in studio and it will be automatically created.

Step 5 repeat the above steps for all modules you want to change the address fields on

The address country field exists on Accounts, Contacts, Leads and Targets by default. But in accounts for example it is billing_address_country and shipping_address_country you will just have to check the field names before makeing the changes.

The countries_dom already exists in sugar and contains the list of countries. This can be edited in the admin panel’s dropdown editior.

Andy
About

Software Developer from Falkirk, Central Scotland.

Posted in SugarCRM development and customisation, SugarCRM development development hints and tips
3 comments on “Convert the country text field in SugarCRM CE or SuiteCRM to a drop-down list
  1. suresh says:

    Hello, i follow those steps but i can’t found those lines in editview.tpl

    what can i do..????

  2. tonyh says:

    Nice one! The bit that I was missing was the smarty template for the select input rather than the test input.

    Cheers!

  3. kalindi says:

    step 5 is not understandable.
    If /custom/modules/[THE_MODULE_NAME]/metadata/editviewdefs.php does not exist just make a change to the editview in studio and it will be automatically created.

    what should i actually do?
    please help me out.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>