Views: Contextual filters

by Gisle Hannemyr

Views' contextual filters let the site-builder filter a view based upon a context provided when a page or block is rendered.

Table of contents

Drupal projects discussed in this chapter: Devel, Views.

Introduction

One of the features of the Views is filters. It allows you to filter the view based upon some value.

Contextual filters extend this concept by allowing the value to be determined by context. A regular filter could be set up to give you all articles written by a specific user specified in the regular filter criterion. A contextual filter may be used to display all articles written by a user whose uid is passed in the path, written by the current user, written by the currently viewed user, or the same user who wrote the currently viewed node.

The values for contextual filter are typically provided by the path. If a view has the path /content-by-uid/article/2 will call the view content-by-uid along with two values for contextual filters (in this case article and 2).

Setting up

Before starting on the examples in this case study, you need to set up your Drupal test site and install the necessary projects.

Install the Views project and enable Views and Views UI.

If your site already has content, you may skip this step and work with that content instead. However, if you're testing this on a newly created test site, you can generate dummy content as follows:

Install the Devel project. Enable the Devel generate module. Create a test user (name it "test"). Navigate to Administration → Configuration → Development → Generate Content and generate 25 nodes each of types Article (machine-name: article) and Pasic page (machine-name: page). They will be randomly assigned to existing users.

Content by user id

Navigate to Administration → Structure → Views → Add new view. Name it "Content by uid". Change the display format to be a Unformatted list of titles. Just leave everyting else set to the defaults.

views_cfl01.png.

Click Continue & edit.

Add a contextual filter. Expand the third column ("Advanced") and next to "Contextual filters", click Add. Select the "Content" filter. Locate "Content: Author uid" and "Content: Type" and select both by ticking the associated box.

views_cfl02.png.

The above image has been simplified. A lot more options will appear and you may need to scroll to locate the items.

Click Apply (all displays).

The next screen sets up the default behavior "Content: Author uid"-filter when the filter value is not in the URL (path). There setting provides a number of options, including adding custom PHP code to compute the value of the filter.

However, in this case study we're going to set it up to provide a default value: The user id from the currently logged in user. The rest are left at the default.

views_cfl03.png.

Click Apply (all displays).

We also need to provide a default calue for the content type. We set this to the machine name for the Article content type (article).

views_cfl04.png.

Click Apply (all displays).

Finally, rearrange the contextual filters. Click on the small down arrow in the menu for contextual filters (to the right of "Add"), and select Rearrage. You rerrange by dragging and dropping the arrow-crosses to the left of the filter. We want content type before uid.

views_cfl05.png.

Click Apply (all displays) to save your new sequence.

Click Save (top right) to save the view.

Now, when a user visits the path /content-by-uid, she or he should see her/his own articles (because the defaults we picked when filters were missing from the URL). If we supply one or both filters, other lists are shown. Examples:

Final word

Here are links to miscellaneous materials about Contextual filters:


Last update: 2016-04-09 [gh].