2

I am trying to create a View to display related content based on shared taxonomy terms from multiple vocabularies.

For example:

I have two content types called Car and Parts.

I have two vocabularies called Type and Country.

Content Types:

  • Car

    • Entity Reference: Type
    • Entity Reference: Country
  • Parts

    • Entity Reference: Type
    • Entity Reference: Country

Vocabularies

  • Type: sedan, convertible, coupe, van, suv
  • Country: usa, japan, germany

I would like to display a view of related Parts when viewing a Car node. Furthermore, I only want to display if both Type and Country match.

I’m able to get either Type or Country matches to display by adding the following relationships.

  • Content: Taxonomy terms on node
  • (term) Entity Reference: Referencing entity (type)
  • (term) Entity Reference: Referencing entity (country)

and a contextual filter of:

  • (Content referencing Taxonomy term from field_country) Content: Nid

Basically, switching the relationship on the contextual filter determines whether or not Type or Country matches should be displayed. I am having trouble figuring out how to display Parts that match both the Type and Country of the Car.

  • u haven't created any relationship between two content types . How can you show related Parts when viewing a car node unless you have some field which connects oe establishes relationship between them – Shreya Shetty Apr 24 '16 at 9:24
  • Do you not need to set the relationships as required? – Daniel Harper Apr 24 '16 at 18:32
  • @ShreyaShetty I have 2 relationships, one for each entity reference field. However the "Content: Nid" contextual filter only accepts one relationship. So whatever field I choose for that relationship is the value that is displayed. – Mark Anderson Apr 24 '16 at 19:00
  • @DanielHarper If I set both relationships to required, then no results are displayed. – Mark Anderson Apr 24 '16 at 19:01
0

Basically you need to do the following.

  1. Create a view based on content of type parts (as a block I'm guessing)
  2. Add a relationship "Content: Country (field_country)" using the defaults
  3. Add a relationship "Content: Type (field_type)" using the defaults
  4. Add a contextual filter for "Taxonomy term: Term ID" with the following settings.
    • Relationship: country
    • Provide default value: Taxonomy term ID from URL
    • Load default filter from node page, that's good for related taxonomy blocks
    • Limit terms to vocabulary: Vocabularies country.
    • Multiple-value handling: Filter to items that share all terms
  5. Add another contextual filter for "Taxonomy term: Term ID" with the following settings.
    • Relationship: type
    • Provide default value: Taxonomy term ID from URL
    • Load default filter from node page, that's good for related taxonomy blocks
    • Limit terms to vocabulary: Vocabularies type.
    • Multiple-value handling: Filter to items that share all terms.

Now just place your block on all car pages. enter image description here enter image description here enter image description here enter image description here enter image description here

  • Thank you so much for taking the time to write such detailed instructions. I tried to follow the steps you outlined, however I do not see "Content: Country (field_country)" or "Content: Type (field_type)" as an available relationship. Do I need to add a different relationship first in order to see those? Also, I should mention again that I am using Entity Reference fields, not Term Reference fields to tag the nodes with taxonomy terms. Thanks again for your time and help. – Mark Anderson Apr 25 '16 at 0:54
  • field_country and field_type just refer to the fields that are on your content types, they were just the field names that I used yours maybe different. I also used the same fields on both content types. – Daniel Harper Apr 25 '16 at 6:55
  • good point about the reference fields I hadn't noticed that :) – Daniel Harper Apr 25 '16 at 7:59
0

I finally got it working by adding the following Relationships:

  1. A bridge to the Taxonomy term entity that is referenced via field_type
  2. A bridge to the Content entity that is referencing Taxonomy term via field_type (add relationship to #1)
  3. A bridge to the Taxonomy term entity that is referenced via field_country
  4. A bridge to the Content entity that is referencing Taxonomy term via field_country (add relationship to #3)

And the following Contextual Filters:

  1. (Content referencing Taxonomy term from field_type) Content: Nid
  2. (Content referencing Taxonomy term from field_country) Content: Nid

All relationships need to be required. And all contextual filters need to have a default value of "Content ID from URL".

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.