Using Drupal Contextual Filters in Views

It can take a while when you're new, but once you start to wrap your head around Views, that is when Drupal gets really fun. In this tutorial, I'll go over how to use Contextual Filters in Views to alter your content dynamically based on information in the URL. If you're a visual learner, you can skip to the video at the end of this post for a detailed walk through of the process.

Kittens vs Specific Kittens

In this example, have page of cat photos. It's made with a standard view that pulls from content type 'Animals' and then narrows it down with a standard filter based on a Species taxonomy list.

A page of kittens

Drupal Contextual Filters allows us to filter content based on dynamic information. It doesn't always have to come from the URL, but that is the most common example. Here's how you do it.

Standard view with normal filters

In the Advanced tab of your view, click 'add' next to Contextual filters. I want to filter on the name of the animal, so I do a search to narrow down the choices and then choose the name field.

Adding the name filter

Now you have some options, the two most important being:

  1. What to do when your filter is not in the URL
  2. What to do when your filter is in the URL

In the first case, I've chosen to just display all the results as normal. In the second case, I've overridden the title of the view and replaced the Previous title 'Kittens' with the actual name of the animal by using the placeholder '%1' which tells Drupal to take the first filter (previously known as 'arguments') in the URL and use it as the title as well. Using '%2' would take the second argument (if there is one) and so on.

Set your filter options

You can test your filter by typing it into the preview window and hitting 'Update Preview'.

Enter your filter into the preview field

If your results narrow to your satisfaction, save the view, then try it out on the page. We should be able to go to sandbox/kittens/[name] and have just one pet show up. Kinda like this:

The final product

Isn't Xander cute?

Pat yourself on the back and have a celebratory cupcake. You're one step closer to Views mastery in Drupal.

thanks for the tutorial. I found it very helpful, but what if you want to group them by name and still want to show the pictures below it?

There's plenty of ways to do that. A block would be the simplest probably, either a custom block or a view block, configured to show up on the requisite pages.

You could also use a views attachment which would then show up everywhere the view you attach it to does.

views attachment option

Hi,

Thank you for the tutorial. It get me started to was I want to do but can get to make the link between my two element. I have a Basic Page content type in witch I put a field type image. I want to create a View-Bloc that will show the image of the Basic Page content type. I have autopath enable also.

I have try to link by the content title without success. Can you help me ?

Thank you

@Jean-Francois I think your question is beyond the scope of what I can answer in a comment but basically you would want to create the block view and under Format -> Show, select 'Fields' instead of 'Content' then you can specifically select your image field and any others you like. Within options for the field you can select 'link this field to it's content' or 'Output this field as a link'

Hope that gets you started and there are also a ton of good tutorials on Views on YouTube and elsewhere. Here's one to get you started:

http://www.youtube.com/watch?v=BebR07kucT4

What if I have a taxonomy reference field with more than one value (for instance in your example there are two kittens in one photo)? How do I use the contextual filter? I used Hierarchical Select to save multiple terms to an article. With an exposed filter, using the dropdown select, it nicely shows the article of my selection, but I want to use the filter from the url (in your example 'Willow' to filter my view. Hope you can help this newbee ;-) Thanx

Hi in drupal 7 i have a view that must filter its result with array of values stored in session. I have used contextual filters with php code to return the filter value but it can return only one value from the array but i have to filter it with all values. Is there any way out...

i have used this code in coptextual filter

foreach ($_SESSION["iteneries"] as &$value){
return $value;
}

Thanks in advance for any help...

First of all, thx for a great tutorial Kronda.

Using your example how would you adjust the context filter if you wanted to filter various animal species types that you had entered in the taxonomy by the letter it started. So instead of placing the full word 'feline', you entered 'f' and it would filter by all species that started with the letter 'f'? So your results would return 'feline' and 'fish' for example.

@ Lud - you should check out in Views, there is a disabled view called "Glossary". That lists content by the first letter. It only shows letters of which you have words. And I think that might give you some clues.

Hi,

its possible to add contextual filters to the default /taxonomy/term/% view (without creating views per term):
like this /taxonomy/term/%/year/month/day - filtering content by creating date,
?

Maczkó, I've been researching quite a bit on contextual links, this might help:

If you want to override the default taxonomy page in Drupal 7, here's how to do it.

Add a new content view with a page display.
Tinker with your fields, filters, and sorting criteria as desired.
Under Page Settings, set the path to taxonomy/term/%.
Add the contextual filter "Content: Has taxonomy term ID."
Under "When the filter value is in the URL or a default is provided," check "Override title" and enter "%1" to return the tag title as the page title.
Save the view
Here's the link I found this at: http://www.webbykat.com/2012/04/overriding-default-taxonomy-page-view-d…

Hi,
Can i use contextual filter using "Alias" Or Any Other Solution ?

How to override a node "book_name_alias"
path is book/book_name_alias

Note: this technique, as presented here, no longer works. If you try to interpolate this technique to the current release of Views 3, you will need to pass the NUMERIC VALUE OF THE TERM ID, not the term, in the URL.

So, if 'kittens' has the term ID 54, you would need to have as your URL 'animals/54', not 'animals/kittens'.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.

About the Author