3

I'm trying to create a view to desplay contents based on shared taxonomy terms.

More concretely, I have two content types:

  • Offers (with field "Category")

  • Needs (with field "Category")

This contents share the same vocabulary "Category". Indeed, the "Category" fields of the content types "Offers" and "Needs" are refered to the same vacabulary (taxonomy) with terms:

  • electronic

  • chemistry

  • mathematics

  • science

Each user can define their offers and needs through 2 differents forms.

To put it simply, let's take the case of 3 users who created their offers and needs using this forms. Here is a table of a concrete example:

enter image description here

==> Case 1: current user (logged in user) = user 1

Imagine that I am user 1 (current user). The view page will have to desplay to me a list of the contents "Needs" of other users (user 2, user 3) and that have the peculiarity of having a "Category" field equivalent to the "Category" field of my contents "Offers".

In other words:

1) My content "Offers" are "Offer 1" and "Offer 2" with fields category ("electronic","science")

2) The contents "Needs" of other users are "Need 2", "Need 3","Need 4","Need 5" and "Need 6"

3) Comparing fields category between my contents "Offers" and contents "Needs" of other users:

  • Field Category of "Need 2" = chemistry. The value of this field is different from the values ("electronic","science"), that are the values category of my offers ==> The view will not display the content "Need 2"

  • Field Category of "Need 3" = electronic. The value of this field is equivalent to the value category of my Offer 1 ==> The view will display the content "Need 3"

  • Field Category of "Need 4" = mathematics. The value of this field is different from the values ("electronic","science"), that are the values category of my offers ==> The view will not display the content "Need 4"

  • Field Category of "Need 5" = science. The value of this field is equivalent to the value category of my Offer 2 ==> The view will display the content "Need 5"

  • Field Category of "Need 6" = electronic. The value of this field is equivalent to the value category of my Offer 1 ==> The view will display the content "Need 6"

4) It means that the view page should display this list of Needs:

  • Need 3 (electronic)

  • Need 5 (science)

  • Need 6 (electronic)

In other words, Offer 1 and Offer 2 (that are my Offers) match with Need 3, Need 5 and Need 6 (that are needs of the other users covered by my Offers). And this because they have the same categories "electronic" and "science"

==> Case 2: current user = user 2

Using the same logic, if the current user is user 2, the view page is going to desplay this list of Needs:

  • Need 5

==> Case 3: current user = user 3

And for the user 3, this list of Needs:

  • Need 1

  • Need 2

  • Need 4

Question : How can I create this view with view UI ? Any other suggestion is welcome.

1 Answer 1

1

This types of matching (list Taxonomy siblings) is usually done through Has Taxonomy term ID Contextual filter, but your case is a bit more indirect.

You're not showing related content on other node pages. You want to list some content on User pages, but the matching is not done between the listed content and the user (because you don't have Taxonomy on users), it is one step more removed, you're matching content through the user "bridge" to other content.

You need to go "the entity reference" way to achieve all the necessary matching and contextual filtering.

First we list the user Offer nodes because they give context, they tell us which categories to look for. Once we have that we pull into Views Needs nodes that share the same category field values as those offers, and we additionally filter out that user Needs, in case they have any, because they're only interested in seeing other people's matching Needs.

  1. Create Page Display Views listing Offers! (filter Content type)
  2. Change Path to user/%user/related-needs
  3. Add Menu Tab (it will show on user profiles)
  4. Add Relationship Taxonomy term referenced from field_category from the Content Category, you can require it since they will all have it filled in
  5. Add a new Relationship Content using field_category from the Taxonomy term Category.

    This Relationship automatically uses the first Relationship. This is where the matching is done from all Offers nodes category back to all the nodes on the system using that same category.

    You have the Title field showing Offer nodes at this point. You can keep it this way just to test, but that field isn't our target since we want to see the list of Needs related to the Offers. Or if you don't need it: modify it according to the following step:

  6. Add another Title Field and make it use the second Relationship (it will be the only one available). This will show the nodes that share the same category taxonomy as the Offer node in that same views row.

    From now on, for every field you add, if you're not using a Relationship it will show Offer node data, and if you use the second Relationship it will show the data from nodes that share the same category as the Offer node from that row. This includes the Needs node we are after.

  7. Add Contextual filter Authored by from the Content Category and configure it:
    Do not use a relationship
    When the filter value is NOT available
    Provide default value
    User ID from route context
    When the filter value IS available or a default is provided
    Override title
    Needs matching user {{ arguments.uid }} Offerings (check your argument token under Replacement patterns)
    Specify validation criteria
    User ID

    This Contextual filters out Offer nodes authored by the user whose profile is being viewed.

  8. Add another Contextual filter Authored by from the Content Category and configure it:
    Relationship field_category (the second one)
    When the filter value is NOT available
    Provide default value
    Raw value from URL
    2
    When the filter value IS available or a default is provided
    Specify validation criteria
    User ID
    More
    Exclude

    This Contextual filter excludes Needs nodes authored by the currently viewed user since it doesn't make sense to show these. This step isn't necessary if your users will never have Needs nodes that share the same category with their Offer nodes.

  9. Since you'll probably have duplicates in your results you can sort it out by turning Aggregation on. If you have Needs fields other than Title you may need to change their Aggregation settings from Entity ID to Target_id.

    Make sure you have all Sort Criteria removed since it interferes with Aggregation.

Visit the user profile page and find the tab, and check that the results are as you'd expect. It seems like a complicated procedure but it makes sense once you understand what is happening in each step.

Your Answer

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

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