Views: Contextual filters
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 article
) and Pasic page (machine-name: page
).
They will be randomly assigned to existing users.
Content by user id
Navigate to
. Name it "Content by uid". Change the display format to be a Unformatted list of titles. Just leave everyting else set to the defaults.Click
.Add a contextual filter. Expand the third column ("Advanced") and next to "Contextual filters", click
. Select the "Content" filter. Locate "Content: Author uid" and "Content: Type" and select both by ticking the associated box.The above image has been simplified. A lot more options will appear and you may need to scroll to locate the items.
Click
.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.
Click
.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
).
Click
.Finally, rearrange the contextual filters. Click on the small down arrow in the menu for contextual filters (to the right of "Add"), and select
. You rerrange by dragging and dropping the arrow-crosses to the left of the filter. We want content type before uid.Click
to save your new sequence.Click
(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:
content-by-uid/page
: Basic pages authored by the logged in user.content-by-uid/page/1
: Basic pages authored by user with uid 1.content-by-uid/article/2
: Articles authored by user with uid 2.
Final word
Here are links to miscellaneous materials about Contextual filters:
- Contextual filters (main entry in Drupal.org wiki).
- Display a parent node and a view of child nodes that reference it.
Last update: 2016-04-09 [gh].