0

The users on my site all have a field named "team" and my content types also have the same field.

I want to create a views page that shows all nodes where that "team" field equals the same field on the user node.

Example:

USERS:

user1, team: team1
user2, team: team1
user3, team: team2

NODES:

node1, team: team2
node2, team: team1
node3, team: team1

When I'm logged in as user 3, I want my view to show node 1.

I know that I have to work with the contextual filters and context but I'm unable to get the output I need.

  • Both the OG Groups module, and the Group module can do this. – Jaypan Jun 26 at 15:48
  • what type is the team field? is that taxonomy term? – Yuseferi Jun 26 at 15:49
  • Sorry, yes, it's a taxonomy term. – zephirus Jun 26 at 16:08
  • @Jaypan both these modules can be a solution but what I need is so simple that I would prefer to not install modules for that. – zephirus Jun 26 at 16:43
2

The easiest way to set this up is through the Content: Has taxonomy term ID Contextual filter.

  1. In Content Views listing your nodes add that Contextual filter and set it up:
    When the filter value is NOT available
    Provide default value
    Taxonomy term ID from URL
    Load default filter from node page, that's good for related taxonomy blocks
    Filter to items that share any term
    When the filter value IS available or a default is provided
    Specify validation criteria
    Taxonomy term
    [your vocabulary]
    Term ID
    Adjust the other option as necessary.

    Now if you enter some taxonomy term ID into the Views Preview field, it will be picked up by the Contextual filter and show only nodes that have been tagged with that term.

  2. Create a block Display and place your block in a region shown on entities that have the taxonomy term ID chosen. The block will show all nodes that share the term.

In your case if you place that block on User Profile pages, it will show the nodes related through the same Taxonomy term.


If you want to show that Block anywhere on the site, and have it take the Context of the currently logged in user you have to approach it a bit differently.

There may be more ways on how to achieve this, here's one:

  1. In the original Views where you show a list of nodes add a Relationship for the Taxonomy term field (Field: Team).
  2. Then add another Relationship that is reverse, going from Taxonomy term to the field on Users (Taxonomy term: User using …). This is where the term matching is done, the second Relationship will use the first one. This introduces the User info into your content Views.
  3. Then add the Contextual filter for User ID (User Uid), it will use the second Relationship. Set up the Contextual to use the logged in user context: 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
  4. Place the Block anywhere on the site and it will only show nodes related to the user that is currently logged in, even if you place it on user profiles and view other users profiles!
| improve this answer | |
  • Thanks prkos. Tried the second part of your answer but I get 0 results. From what I understand the view is of type content, not user right? – zephirus Jun 29 at 10:28
  • Yes, it should be Content type View if you want to show/list nodes. The user is just the context for which nodes to show. It's impossible to figure out what might be wrong. Go step by step and test each change that it does what it should. Add User field using the second Relatinship to test that it matches correctly. Only then add the Contextual filter, you can test that Contextual in a new Test views without Relationships. – prkos Jun 29 at 14:44
  • Solved. I think I was missing something and I don't know what. – zephirus Jun 30 at 13:40
  • Yes, sometimes IT problems just work the second time you try :) I'm glad it's working now :) – prkos Jun 30 at 14:40

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.