0

The premise: I have a taxonomy listing “countries” on the first level and “country subdivisions” on the second level.

E.g.:

  • Canada
    • Ontario
    • Quebec
    • etc..
  • United States
    • Alabama
    • Alaska
    • etc..

A user is attributed to a country in the user Account Settings. E.g: John Doe is from Canada.

I would like to create an Entity Reference View that would list all Provinces related to the user attributed country.

E.g.:

List of provinces offered to John Doe while creating a node:

  • Ontario
  • Quebec
  • etc..

1 Answer 1

2

First you have to create the Taxonomy hierarchy within Views, so later only the children terms will be in Views rows results (so they can be chosen from a ER field on the node creation form). You can't do that starting from User Views because only Parent Relationships are available, no way to drill down, only up is possible.

  1. Create Taxonomy term Views of your particular Vocabulary. They will list both Parents and Children. It will already have the Taxonomy Name field, keep it.

  2. To create the hierarchy add the Relationship Parent first (It's called "Taxonomy term", search for "parent" to find it quickly in the Relationships list. Require it because we only want to display terms that have a parent, we only want children terms to show on our list

  3. Then add another Relationship, introducing the User, it will be named something like User using field_country, make it use the Parent Relationship (so we get users who are tagged with top-level terms). Require the Relationship because we only need terms that do have users tagged with them.

  4. Now add the Contextual filter User ID. It will automatically use the Country Relationship, and configure it:

    When the filter value is NOT available
    Provide default value
    User ID from logged in user
    When the filter value IS available or a default is provided
    Specify validation criteria
    User ID
    Single ID
    Hide view or what ever makes sense in your case

    To easily test whether the correct sub-terms appear enter some user ID into the Views Preview field. It will provide the context directly to Views so you can see which sub-terms are shown and whether they match that user Country.

  5. Add the Entity Reference Display to your Views and adjust its Settings, (Search fields: Name). Save the Views so you can use it in the next step.

  6. Create/Edit Taxonomy Reference Field on your CT, select the Views as the Reference Type and then select the Views you've created in the first step.

  7. Adjust the "Manage form display" for the new field so it appears as you like it on the node create page.

Now when you go to create a new node of that type the field will list all the sub-terms of the Country the author is tagged with.

You still have to test by logging in as some user to test that the context is passing through correctly (Switch user block can speed it up).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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