| Drupal

A-Drupal-Dropdown-List-of-Countries-States-CitiesOne of our members wanted to create a taxonomy of Countries, Provinces and Districts.

For example, you might choose England, then Yorkshire, then Leeds. Or you might choose Canada, then Ontario, then Toronto. You get the idea.

In this tutorial, we'll use the Hierarchical Select module to make that happen.

Step #1. Get set up

First, you need to set up your taxonomy. I've a simple example here with Countries and Cities, but you'll probably add a lot more terms.

media_1407964548074.png

Next, install and enable Hierarchical Select: https://www.drupal.org/project/hierarchical_select

Make sure to enable "Hierarchical Select Taxonomy":

media_1407964492896.png

Step #2. Create the field

  • Go to Structure > Content types > Manage fields.
  • Create a new field. Choose "Term Reference" and then "Hierarchical Select":
media_1407964599338.png
  • Save the field.
  • Click the "Hierarchical Select" link.
media_1407964692305.png

You'll now see a series of options for the field:

  • Save lineage: Save all term levels (City and Country) or just the one the user chooses.
  • Level choice: What level can a user choose from? Only City? Other other levels too?
media_1407965480092.png

Under "Level labels" you can give labels to the different levels of the taxonomy:

media_1407965646947.png

Under "Dropbox settings", you can decide how many choices a user can make:

media_1407965687740.png

And under "Editability settings", you can decide whether the user can add new terms and levels to our set-up:

media_1407965782913.png

Step #3. Use the field

Finally, click "Add content" and use your field. Here's how the top level will look:

media_1407964776177.png

And, once the first level has been selected, here's how the second level will look:

media_1407964831158.png

About the author

Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.