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.

Comments

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?

kronda's picture

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

What if the content name had diacritics (joão) or spaces and punctuation (mr. jones)? How would it show up in the url?

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

kronda's picture

@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...

Add new comment