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:
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,
LikeLike
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.
LikeLike
Stuck on step 7. I add a new article, but city field doesn’t get updated whenever I select a Country field.
LikeLike
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?
LikeLike
Does this work as exposed filter ? I tried it but it doesn’t work.
LikeLike
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.
LikeLike
Thanks very much for this! I was struggling to get this working before I found your tutorial.
LikeLike
Thanks for you comment~~ =) and i want to have swissroll today~ =P
LikeLike
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).
LikeLike
I didn’t try to use Hireachy Select with Field Collection. But i find a similar issue where you can find the patch. See if that could work.
Issues when Hierarchecal Select is inside Field Collection
LikeLike
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.
LikeLike
Sorry that i didn’t read the context carefully. i have created a patch. see if that works for you.
Limiting options doesn’t work with field collections – #12
LikeLike
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 😦
LikeLike
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 ?
LikeLike
i modified the code so that in will check whether the fields are inside the field collection. In my case, it works if both country and city are outside the field collection field as well as both inside the field collection field.
you can refer to my modified Reference field option limit module folder in the reply below.
Limiting options doesn’t work with field collections – #17
LikeLike
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 ?
LikeLike
I applied the patch that is available here at https://drupal.org/node/1649470#comment-6843032 . It worked fine if the field collection is only one but if the field collection is created with unlimited option then only for the first field collection reference it works but rest of the others the second child select list shows up all the options.
LikeLike
thanks for posting that patch. good to know you get it work and it’s not easy to make the module work for multiple values.
LikeLike
‘Taxonomy Term Reference Tree Widget’ (term_reference_tree) can filter terms pased on the parent.
LikeLike
Thanks for your suggestion, will try it later if i need to work on that kind of feature~ =D
Drupal Module – Taxonomy Term Reference Tree Widget
LikeLike