How to use contextual filter and relationship in views

Submitted by hemant.gupta on Mon, 05/16/2016 - 09:44

Views is a very powerful module of Drupal. You can use it to show data in various ways which is not easily possible. Today we are going to tell share with you the two advanced and powerful features of views namely Relationships and Contextual filters.

Relationships

Relationships are basically a fancy way of saying that table A will join with table B in order to retrieve data related to it from there and the join will happen on some common field in both tables.

For Example: To make a View that shows all the articles and username of their author.
To make this view first we will have to create a simple view showing only the titles of all the articles. Now we have to add the second field of the author name. The node table only contains a reference to the user entity that created the node (in the form of a user ID – uid). So that’s pretty much all we will find if we look for user related fields: Content: Author uid..For getting author name what we need to do is use the relationships to the user entity found in the user table.

So let’s go ahead and add a new relationship of the type Content: Author.. Under Identifier, we can put a descriptive name for this relationship like Content Author. The rest we can leave as default.

relationships

Now if you go and add a new field, you’ll notice many others that relate to the user who authored the content. Go ahead and add the User: Name field. In its settings, you’ll see a Relationship select list at the top where the relationship identifier we just specified is automatically selected. That means this field is being pulled in using that relationship (or table join). Saving the field will now add the username of the author, already visible in the View preview.

You can extend this method to join as many tables as you want.

Contextual filters

Contextual filters are similar to regular filters in that you can use mainly the same fields to filter the records on. Where contextual filters differ greatly is that you do not set the filtering value when you create the View, but it is taken from context.

There are many different contexts a filter value can come from, but mainly it comes from the URL. However, you can instruct Views to look elsewhere for contexts as well – such as the ID of the logged in user.

Now we will use the contextual filter to show the list of articles written by the currently logged in user.
Add a new contextual filter of the type Content: Author uid. Next, under the "WHEN THE FILTER VALUE IS NOT IN THE URL" fieldset, select the Provide default value radio.You then have some options under the Type select list, where you should choose User ID from logged in user.

contextual fiter

This will make Views take the ID of the user that is logged in and pass it to the View as a filter. The rest can be leaved as default and then save it and you will see that only the articles written by you are showing up.

Now if you will logged in from other user account then it will show the articles written by that user. The contextual filters can be used to filter data displayed on the page view based on the parameters passed. For example let us say you are working on an alumni portal of a school or college where you want to display the alumni directory. Now this page view is accessible at /alumni path. Now you can pas arguments to this path in a following manner.

/alumni/GENDER/BATCH

You can add Gender and Batch field as contextual filter and this page view's data can be filtered based on the argument passed in the url. Really handy!

So here it is the 2 useful and powerful concepts of views very useful for showing your data.