Skip to main content

How to Limit Reference Field Option in Drupal 7

This tutorials is about how to limit reference field options in Drupal 7 hierarchy taxonomy.

In Drupal 7, taxonomy list selection is not very user friendly if you have a hierarchy structure of taxonomy such as:
    - Connaught Place
    - Saket
    - Vaishali
    - Vasundhara

In the above case, most suitable solution would be to have two separate taxonomy vocabularies - one for City and other one for City Location and the appearance of city locations should depend on the selection of city name. Apparently, there is a module called "Hierarchical Select" in Drupal which does this job of multiple selection list for taxonomy but it doesn't quite do what we actually expect. We'll be achieving the above discussed result using "Reference field option limit" module. You should download this module and install and enable it before going through all the process.

1. Add Vocabulary

- Create a vocabulary called City with the following 2 terms -

- Create second vocabulary called City Locality with following 4 terms -
- Connaught Place
- Saket
- Vaishali
- Vasundhara

2. Edit Vocabulary and Manage Fields

After you have added the above two Vocabularies, click on "edit vocabulary" associated with City Locality. Click on "Manage Fields" tab and add a new field of type "Term reference" and set the fields as follows
- Label: City
- Field Type: Term reference
- Widget: Select term

Now, Click on "Save" to create the new field. Setting of the next screen will be as follows:
Field Setting: City and click on "Save field settings" button at the bottom of the screen. You don't need to change anything on this page. The Final screen should look like this if you're using the same naming convention for your practice.


3. Reference the terms from "City Locality" vocabulary to "City" vocabulary

For example:
Connaught Place (reference it to Delhi)

You'll have to open each term in "City Locality" vocabulary to Edit and reference them to respective terms in "City" vocabulary. This process has to repeat for all the terms in "City Locality" vocabulary. If you're yet to add new terms to "City Locality" vocabulary, you can reference them as you create them.

4. Add Both Vocabulary to Node

- Open the Content type for Manage field so that you can add these two vocabulary term references -
Now, go to Administration -> Structure -> Content Type and click on the "Listing" (In this case, I have a node type called "Listing" setup for the purpose of this example but you should select one of your choice).

While inside the Listing content type management page, click on the "Manage Fields" tab.
- Add existing field:
- Select "Term reference: field_city" from the Existing list of fields and save the setting. There is no setting required in this step. Let the default settings unaltered but make sure the "City" is selected in Vocabulary setting.

Now, Add another term reference for City Locality field -
Label: City Locality
Field Type: Term reference
Widget: Select list
Click on "Save" and select the vocabulary "City Locality" and click on "save fields settings".
After that, check the box for following as depicted in the following screenshot below -
- "Limit this field's options according to matching fields values" and
- "City (field_city)"
- "Hide all options if nothing is selected in the matching field(s)".
and finish it with saving the form

NOTE: If you haven't installed the module (i.e. Reference Field Option Limit) yet, this option wno't show up. So, download, install and enable it now. After that go back to the "Manage Field" tab of the Listing content type and click on the Edit link of the "field_city_locality" field and start from checking the appropriate boxes as explained above.
8. You'r done. Now, go to create new listing content and you should see this configuration in action -the City Locality would change as per the City selected.