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
limit_location_countries.info
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
limit_location_countries.module
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 =)
Reference:
- How to alter country dropdown in location module?
- Give administrators the ability to limit country options