Drupal 7 – Limit the number of countries in the country list of the Location module

If you want to include location information in your content type. The Location module could help. But by default, the list of selectable countries are very long and sometimes we may want to limit it to a few countries only. This can be done by creating a custom module but it only works for Location CCK but not for Node Location and User Location

1. Download and enable the Location and Location CCK modules

2. Create a location field in your content type (The content type in this example is called service)

3. Create a custom module called limit_location_countries

name = Limit location countries
description = Limit the number of available countries in the Location module by form alter.
core = 7.x
package = Eureka
version = 7.x-1.0



function limit_location_countries_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'service_node_form') { // modify the form id for different forms
    $form['#after_build'][] = 'limit_location_countries_after_build';

function limit_location_countries_after_build($form_element, &$form_state) {
  // Replace FIELD_NAME with the machine name of the location field for your content type
  $form_element['FIELD_NAME']['und'][0]['country']['#options'] = array(
    'All' => '- Any -',
    'ca'  => 'Canada',
    'gb'  => 'United Kingdom',
    'hk'  => 'Hong Kong',
    'us'  => 'United States',
  return $form_element;


4. Enable the module and it should work now~

Done =)


4 thoughts on “Drupal 7 – Limit the number of countries in the country list of the Location module”

  1. For distance/proximity filter, this is what I did and it worked:

    function limit_location_countries_form_alter(&$form, &$form_state, $form_id) {
      if ($form_id == 'views_exposed_form') { 
          $view = $form_state['view'];
            if ($view->name == 'view_name' && $view->current_display == 'current_display_name') {
                $form['#after_build'][] = 'limit_location_countries_after_build';
    function limit_location_countries_after_build($form_element, &$form_state) {
      $form_element['distance']['country']['#options'] = array(
        'ar' => 'Argentina',
        'au' => 'Australia',
        'at' => 'Austria',
        'be' => 'Belgium',
        'br' => 'Brazil',
        'ca' => 'Canada',
        'dk' => 'Denmark',
        'fr' => 'France',
        'de' => 'Germany',
        'hu' => 'Hungary',
        'it' => 'Italy',
        'mx' => 'Mexico',
        'nl' => 'Netherlands',
        'pe' => 'Peru',
        'pl' => 'Poland',
        'pt' => 'Portugal',
        'ru' => 'Russia',
        'sk' => 'Slovakia',
        'es' => 'Spain',
        'ch' => 'Switzerland',
        'tr' => 'Turkey',
        'gb' => 'United Kingdom',
        'us' => 'United States',
      return $form_element;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.