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:

Thanks for that great tutorial.
But how can I limit also the list on the views “Distance / Proximity search filter” ?
LikeLike
Yes you can. I did try the Distance / Proximity search filter but for some reasons, it doesn’t work well if you expose the filter.
And you could also try this
Drupal Module – Geolocation Proximity
LikeLike
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; }LikeLike
Thanks for your comment. =)
LikeLike