Drupal 7 - Create taxonomy based Countries list in 3 easy steps

Sun, 09/15/2013 - 16:11 -- meladawy

I'd like to share with you these simple steps to create taxonomy based countries list that contain country name and code

1- Create new vocabulary with machine name "countries".

2- Add new field "field_country_code" to the vocabulary, which contain the country code.

3- Execute the following PHP code inside your theme template files or using Devel module.

<?php // Don't use <?php tags with Devel module
// We request this pretty txt file from www.iso.org which contain list of countries with code name
$countries_list_request = drupal_http_request("http://maged.me/country_names_and_code_elements.txt"); 
// Make sure we have requested the URL Successfully
if($countries_list_request->code == 200) {
$countries_list = explode("\n", $countries_list_request->data);
foreach($countries_list as $country_content) {
	// Skip first row Country "Name;ISO 3166-1-alpha-2 code"
  if(strpos($country_content, 'Country Name') !== FALSE) {	
  continue;
  }
  //  Extract country name and code from the text
  $country = explode(";", $country_content);
  if(count($country) == 2) {
  $country_name = $country[0] ; 
  $country_code = $country[1] ;
  // Create new taxonomy term of type "countries" and field "field_country_code"
  // Replace "countries" with vocabulary machine name
  $countries_vocabulary = taxonomy_vocabulary_machine_name_load("countries") ; 
  $term = new stdClass();
  $term->name = $country_name;
  $term->vid = $countries_vocabulary->vid;
  // Replace "field_country_code" with country code field name
  $term->field_country_code[LANGUAGE_NONE][0]['value'] = $country_code ; 
  taxonomy_term_save($term);
  }
}
}
?> // Skip this line with Devel module

Aaaand finally...We have the list of countries :)

Good Luck,
Maged Eladawy

Comments

meladawy's picture

Its discussed somewhere else in the community...but i see its overhead to install such module for this purpose. my way is faster and doesn't require any custom module 

Tried this but cant get it to only create 1 term per country. Instead it generates 2 of each.

Hi, thanks for your explanation. The iso.org file you mention must have moved, it throws a 404. So I rewrote your script with this handy Drupal API function : country_get_list(). It provides an ISO code as well.

Here is the code :

$countries_list = country_get_list();

foreach($countries_list as $iso_code => $country) {
$countries_vocabulary = taxonomy_vocabulary_machine_name_load("countries");
$term = new stdClass();
$term->name = $country;
$term->vid = $countries_vocabulary->vid;
$term->field_country_code[LANGUAGE_NONE][0]['value'] = $iso_code ;
taxonomy_term_save($term);
}

Regards

meladawy's picture

Awesome method...i have also replaced the old url with another one 

http://maged.me/country_names_and_code_elements.txt

Thank you !

Hi
Please help me.Your source text file does not wok now.Can you update this as soon as possible?
I'm not meet the trouble for about that.
Help me
Pls
Thanks

Useful hint, ty

$countries_list = Drupal::service('country_manager')->getList();
foreach($countries_list as $iso_code => $country) {
$term = \Drupal\taxonomy\Entity\Term::create([
'vid' => 'countries',
'name' => $country,
'field_country_code' => $iso_code,
]);
$term->save();
}

thanks, guys

Very useful tip, many thanks! This is incredibly useful if you want to filter a list by country with entity reference filter!

I am not a developer but drupal amazes me daily. There is a module for everything. I like your approach. I am creating a social network and want to have Countries, states, cities, addresses, address autocomplete and all the stuff you know you see on other social networks.

What modules and tutorials would you suggest? Do you have anything for me?

You have a nice website. I really like the way comments showing up :)

meladawy's picture

Hey Umair, 
Thank you for commenting on my little blog, you can use field_collection module to handle dependency fields 
https://www.drupal.org/project/field_collection

But i don't think it will work with country & city fields. I usually create a custom module that customize country and city dependency using Drupal Ajax API. This reqruie development effort for sure. 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.