Drupal 7 – Reference field option limit for hierarchy taxonomy

Update @ 2013-11-28: You can also consider using Taxonomy Term Reference Tree Widget suggested by kenorb. =D

When you have a hierarchy structure of taxonomy, the default selection list is not quite user friendly because it list out all the parent and child terms. Assume with want to attach a country and city terms to the Article content type. The most straight forward way is create one vocabulary with some countries as parent terms and some cities as child terms like the following list.
  — England
    — London
    — Manchester
  — France
    — Lyon
    — Paris

It turns on that when someone is adding a new article, he may select either a country term or a city term. That is probably not what we want.

A better solution is provided by the Hierarchy Select module which we have talked about it in the past.
Drupal 7 – Mutiliple selection list for taxonomy using Hierarchical Select

Unfortunately, the Hierarchical Select does not support Views currently. So today i would like another approach which also works with Views and it is the Reference field option limit module.

1. Download and enable the module.
 

2. Create a vocabulary called Country with the following 2 terms.

  • England
  • France

 

3. Create a separate vocabulary and name it as City, add a new term reference called field_country so now each city term has to select a specific country term.
 

4. Create the following city terms.

  • London (field_country set to England)
  • Lyon (field_country set to France)
  • Manchester (field_country set to England)
  • Paris (field_country set to France)

 

5. Add the existing field_country to the Article content type as usual.
 

6. Add a new term reference field with field name field_city and in the setting page, check the Limit this field’s options according to matching field values option as depicted in the following picture.

 

7. Now when u add a new article, the City field will get updated whenever u select a Country field.

 

For views, you can expose both the Country and City term filters. So it should work with Views without any problem. But currently there is a bug in the module and the AJAX update of the exposed city term (child term) does not work in Views. Here is the tracking issue in the Reference field option limit project.
Error in views on adding a field with (option limit) as filter criteria

And this Reference field option limit is not limited to term reference, it also supports the entity reference fields.

Done =)

Reference:

20 thoughts on “Drupal 7 – Reference field option limit for hierarchy taxonomy”

  1. not too sure if this is exactly the same case, but when i have a hierarchy taxonomy list, i create a single term reference field for the content type. and i added some js to make it so when u select Paris, it will select France as well.

    a piece of code like this,

    (function ($) {  
      $(document).ready(function() {
        // Create array with top level terms.
        $('.field-type-taxonomy-term-reference.field-widget-options-select select[multiple="multiple"] option').each(function(){
          $(this).bind('click', function(){
            if ($(this).text().substr(0, 1) == '-') {
              var prevOption = $(this).prev('option');
              while(prevOption.text().substr(0, 1) == '-'){
                prevOption.attr('selected', true);
                prevOption = prevOption.prev('option');
              }
              prevOption.attr('selected', true);
            }
          });
        });
      });
    }) (jQuery);
    

    Like

    1. I try to did similar things in a Drupal 6 website too. I used js to disable some of the city options when a country is selected. In Windows, disabled options are hidden in the selection list but in Mac, the disabled options are greyed. so i think this module provides a better solution as it implement ajax to update the selection list.

      Like

  2. Stuck on step 7. I add a new article, but city field doesn’t get updated whenever I select a Country field.

    Like

    1. Did u select country as the matching field?

      This module requires Drupal core 7.8 or higher. And is there any error log in you webserver or the drupal log?

      Like

    1. It should work with views as exposed filters if you exposed them as different filters. But if you want to retain the ajax feature provided by this module, probably u need to following this issue and applied some patches.

      Like

  3. Do you have any suggestions that how to implement this into field collection ? I implement this outside field collection it works fine but inside field collection it throws error like this :

    Notice: Undefined index: und in reference_option_limit_form_alter() (line 417 of /home/mohan/project/drupal-7.22/sites/all/modules/reference_option_limit/reference_option_limit.module).
    Notice: Undefined index: und in reference_option_limit_form_alter() (line 437 of /home/mohan/project/drupal-7.22/sites/all/modules/reference_option_limit/reference_option_limit.module).
    Notice: Undefined index: #description in reference_option_limit_form_alter() (line 437 of /home/mohan/project/drupal-7.22/sites/all/modules/reference_option_limit/reference_option_limit.module).

    Like

  4. Actually my concern is for the reference_option_limit it shows this error inside the field collection I also saw some issues in the drupal forum about reference option limit it seems this is a bug and has not been addressed.

    Like

  5. Thanks for the patch I applied it but the child select list is not able to populate in the field collection. After the parent select list is selected it makes ajax request but the second child select list is not shown up 😦

    Like

  6. Yes, the content type is similar to the one that is in your blog. I applied your patch in the dev version of reference option module. It is fine outside the field collection but inside the field collection it cannot populate the second child list. By the way how did you achieve this inside field collection ?

    Like

  7. I tried with the attached refernce option module that you provided but still my problem persists. It is not able to populate the second field inside field collection even though it made an ajax call. Since the error does not show up but no results in the child list. Yes it works fine outside the field collection. By the way which field collection module you are using stable or dev version ?

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.