Filtering a Drupal 7 View based on a user profile field

The exact question answered here is “How do I filter a View based on the value of a taxonomy term reference field in the current user’s profile?”

This is covered in quite a few places on the web, but I just wanted to create good step-by-step instructions on how to actually do it, in a place where I can find the instructions easily. So they’re here:

We’ll start by assuming that we have already created a Taxonomy vocabulary called “Fruit”, with some terms as well. The Article content type should have a term reference field referring to the Fruit vocabulary, as should the user account.

1) Create a new View

– Admin -> Structure -> Views -> “+ Add new view”
– Name “My test view”
– Show “Users”
– Defaults are fine for the rest, click on “Continue & edit”

2) Add relationships

– “> Advanced”
– “Relationships add”
– “User: Fruit (field_fruit)”
– “Apply”
– The default identifier is fine
– Check “require this relationship”
– “Apply (all displays)”

– “Relationships add”
– “Taxonomy term: Content with term”
– “Apply (all displays)”
– “Relationship: term from field_fruit”
– “Apply (all displays)”

3) Add contextual filter

– “Contextual filters add”
– Check “User: Uid”
– “Apply (all displays)”
– Select “Provide default value”
– Type “User id from logged in user”
– “Apply (all displays)”

4) Define fields for the view
– Under “Fields” click on “User: Name”
– “Remove”
– “Fields add”
– “Content: title”
– “Apply (all displays)”
– Defaults are fine, “Apply (all displays)”

Easy as pie! Right?

Edit: If you want to filter by multiple terms so that all terms must match, see my next post.

This entry was posted in IT and tagged , . Bookmark the permalink.

14 Responses to Filtering a Drupal 7 View based on a user profile field

Leave a Reply

Your email address will not be published. Required fields are marked *