drupallocationhook-form-alter

How to alter country dropdown in location module?


I have been researching a way to limit the available countries in the drop-down that comes with the contrib locations module. I think hook_form_alter is the way to handle just showing certain countries, but starting a hook_form_alter snippet from hand is not something that I have the ability to achieve. After much googling I have not been able to find a code snippet to get me started.

A project I am working on now only allows registrations from the US and Canada, so I want to limit that drop-down to just those 2 countries. The function that calls the country list is location_get_iso3166_list and the array is $countries. The location module is being used to populate pieces in the Content Profile module.

I have found a couple posts online that suggest just commenting out the countries that are not needed in the .inc file...this is not an option for this project as we are on a multi-site set-up, so changing it in the module will affect other sites. I think I need to add a hook_form_alter snippet to the template.php

Any help is greatly appreciated.

Thank You! -Jeff


Solution

  • You are correct, hook_form_alter() is a good start. If you are looking to alter a content type form, one method I have used is to create a really small and simple custom module implementing hook_form_alter(). Details/instructions on creating this module can be found below.

    As an example, I am calling this module 'custom_countries', if you want to change the name, you can always rename the files and do a search and replace in them later.

    First you need to create a new folder in your modules folder (sites/all/modules, etc). (All files created from now on should be placed in this folder). Next, create a new file called custom_countries.info and put the following inside and save:

    name = "Custom Countries"
    description = "Changes the list of countries available from Location module for certain content types"
    core = 6.x
    

    Next, create another file called custom_countries.module, place the following code inside and save the file:

    <?php
    /**
     * @file custom_countries.module
     * Module to change the countries options of location module
     * for certain content type(s)
     */
    
    /**
     * Implementation of hook_form_alter()
     */
    function custom_countries_form_alter(&$form, $form_state, $form_id) {
      // Replace "YOUR_CONTENT_TYPE with the name of the content type desired
      if ($form_id == 'YOUR_CONTENT_TYPE_node_form') {
        $form['#after_build'][] = 'custom_countries_after_build';
      }
    }
    
    /**
     * Make changes to countries field after all fields are rendered
     */
    function custom_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][0]['country']['#options'] = array(
        'ca' => 'Canada',
        'us' => 'United States',
      );
      return $form_element;
    }
    

    Important: be sure to read the comments and change 'YOUR_CONTENT_TYPE' to the machine name of the content type your location field is in (possibly just 'profile' if using default content_profile settings). Also, change 'FIELD_NAME' to the machine name of the location field.

    Finally, enable the module at admin/build/modules.

    Now when you are creating/editing the content type you specified, you will only see 2 options in the countries list. Using this method, you can now easily make changes to other forms as well.

    The idea for this came from Make Location form fields available to hook_form_alter(). If in the future you decide to add other countries, the complete list of key/value pairs can be found at http://api.lullabot.com/location_get_iso3166_list/5