6
  1. I have Taxonomy called "Region" ex: (Region A / B / C)
  2. Each user is linked to Taxonomy "Region" using Profile2 module (one person can be linked to multiple regions)
  3. Content Type "Article" has a Term Reference field linked to same Taxonomy. (value = unlimited)

Requirement: . In content type "Article", I am asking user to select the Region (as Taxonomy Select list) where this Article belong too - how do I limit the Taxonomy list based on the Region user have an access too?

ex: User1 is linked to Region A / User2 is linked to Region A & B; When User1 creates an Article, Term Reference Taxonomy to show "Region A" but when User2 - show both "Region A" and "Region B"

in SQL world, I would write something like this.

select t.name from taxonomy t where t.name in (select p.name from profile p where user_id = :%1);

How would you achieve this in Drupal without writing custom code?

p.s: My apologies - I am from database world and very new to Drupal; still learning Drupal lingo.

3

I achieve the behaviour described in the question with:

  • Entity Reference
  • Views

In my Entity Reference field I use these settings:

  • Target type: Taxonomy term
  • Entity selection > Mode: Views: Filter by an entity reference view
  • Entity selection > View used to select the entities: My custom view

In My custom view entity reference I have (most importantly):

  • A relationship: to the user referencing the taxonomy
  • A contextual filter: to grab only the terms the user "is linked to"

Related to this question - actually this is what brought me here, In some cases it easier to filter taxonomy terms using a get parameter. For example, I want my visitors to see only the cities of one country when adding a classified ad, I will have a view that produces this:

  • Morocco: q=node/add/ad?country_tid=1
  • India: q=node/add/ad?country_tid=2
  • Zambia: q=node/add/ad?country_tid=3

And on the entity reference view used on the ad content type I will add a contextual filter with a default value generated by this PHP code

return $_GET['country_tid'];

PS. I tried hierarchical select, term reference tree and hook_form_alter. This solution is KISS.

2

There's a module named Term Reference Tree, which - apart from a nice tree widget - provides an option to filter presented taxonomy items through a result of a view. I mean, I never had to use that option, but I assume it's working ok. However I think you'd have to resort to using that widget then, as well.

  • Thanks Artur, in my dummy this .. it worked. Sharing for others: 1) Create a view, which list Region based on the user who is logged in (using Relation link in View) 2) content type article, add a new field type=Term Ref then select Term Reference Tree / under field setting select filter by the view created in #1 and Whola!! Love Drupal :-) – user7079 Apr 30 '12 at 1:59
0

If you want to do this without the term reference tree module try the http://drupal.org/project/entityreference module. Just create an entity reference field and you can limit using views.

0

It worked for me with the following steps

  1. Create a view, which list Region based on the user who is logged in (using Relation link in View)

  2. content type article, add a new field type=Term Ref then select Term Reference Tree / under field setting select filter by the view created in #1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.