Sunday, April 30, 2017 06:49 am EDT

Drupal 7 Views Contextual Filters to Create a Summary of Taxonomy Terms

Bri's picture

Here I want to setup a page with a few views. I want to create a page of node teasers of some sort, perhaps with some exposed filters, but I also want a block that shows a summary of my nodes. I want the block to show how many nodes are in a particular taxonomy. This is nice to do with a taxonomy of States or "types" and acts as a "view nodes by type" action. This can be done with a regular text field as well, but if you are using a taxonomy it is a little trickier. Here are some basic instructions to create a page that looks like the following:

First you are going to want to create a basic page view. This can be pretty simple and display the teasers of your content type. The details are up to you. The view might looks something like this:

Next you will want to set up the block that will serve as the filters for your content type. The block should display the taxonomy field that is being used to classify the nodes of your content type. Here is an overview of that view:

The important part here is the contextual filter of the block. The field should be the term reference field for your taxonomy. The following shows the settings you will want. First select that you want to "display a summary" when the filter value is NOT available.

Next you will want to make sure that the option to display the record count with the link is checked.

Now you are ready to set up the validation requirements for when the filter is present. Check the box to specify the validation criteria, and choose Taxonomy Term as the validator. Check the box for the vocabulary that the taxonomy terms belong to.

Finally, you will want to fill in the last few drop downs. First is the filter value type box. This should be set to "Term name converted to term id" and that will give you URLs that use the term name rather than the term id. For instance, in my example, my page is at /resources and my filters will be /resources/colorado rather than /resources/10 where 10 is the term id. When using this option you probably will want to check the checkbox for "Transform dashes in URL to spaces in term name filter values". Lastly select the action for when the filter doesn't validate, and which action to take.

Once you save these views and put them on a page together, you will have something that looks like the following:

This gets us most of the way there, but as you can see, by default, the taxonomy ids are being displayed in the block, rather than the term names which is what we are ultimately after. To get the last part we need to add a little change via a preprocess function in the template.php file. Add the following to your template.php file in your theme. Be sure to change the parts in brackets to the values that correspond to your theme and views.

 * Used to change the term id's on the resources block to term names instead
function [THEME_NAME]_preprocess_views_view_summary(&$vars) {
  if($vars['view']->name == '[VIEW_NAME]' && $vars['view']->current_display == '[CURRENT_DISPLAY_NAME]') {
    $items = array();
    foreach($vars['rows'] as $result){
      if(is_numeric($result->link)) {
        $term_object = taxonomy_term_load($result->link);
        $result->link = $term_object->name;
        $items[] = $result;
      else {
        //used for the no-value item
        $items[] = $result;
    $vars['rows'] = $items;

If you save this file and clear your cache, you should come up with the names of your taxonomy terms instead of the term ids. If your block has a category of <no-value>, you can remove this by making the taxonomy a required field on the content type, and ensure that all nodes have a term filled in for it.


Anonymous's picture

Receiving array_flip() Error

Man this article has been very helpful even almost 2 years afterward. I am able to display my taxonomy terms with proper contextual links but I'm receiving the following error on my view page.

Warning: array_flip(): Can only flip STRING and INTEGER values! in views_plugin_argument_validate_taxonomy_term->process_summary_arguments() (line 202 of /Users/me/Drupal Site/sites/all/modules/views/modules/taxonomy/

Again, everything seems to be working as expected but I just have that big red error on my view page. Any suggestions? Thanks

Anonymous's picture

This error is unrelated to

This error is unrelated to the code above. You can fix it by filtering out content without a value for this vocabulary.

Bri's picture


What version of core and views are you using? I know this code is pretty old, but I did use it not too long ago on a different project and it was working well for me without any kind of errors.

Anonymous's picture

Taxonomy term name with ampersand ( &) issue?

Hello BRI,

Thanks for the great post and it works fine for all terms but if term has
ampersand ,in this case term name is going to page not found ?

Ex for Term name is : Caribbean & Mexico

Please help me

Here is My mail id:

Thanks in advance

Bri's picture

Should not be a problem

Hi there,
This should not be a problem for you. The core should take care of prepping the paths that have ampersands in them such that when you access the URL, it is converted to something like "Caribbean-%26-Mexico." I just tested it out on a fresh install of 7.22 and it worked for me.

Anonymous's picture

Thank 4 nice example.

Thank 4 nice example.

Anonymous's picture

drop down list with term names


I am trying to make the taxonomy list to be in drop down list with a "Go" button instead of a list. When i select "jump menu" under Contextual Filter>Format, the taxonomy id are displayed instead of term name. Any idea how to change the code to make it show the term names instead of taxonomy ids in drop down list?


Bri's picture

Never Done It

I have never actually used a jump menu with a summary, but I don't see why you can't theme it how you like with a views template. I might be able to give it a shot later. If you are still having trouble let me know and i'll try to throw together an example.

Anonymous's picture

I saw your first screenshot

I saw your first screenshot that you have two drop down list for State and Partner. I guess it is something like a filter. I am doing something like that with the method that you shared above. Everything works fine. But if I use "jump menu" instead of "list" under Contextual Filter>Format, the drop down list is showing taxonomy id rather than the term name.

Do you mind to share with us how you create the filter? Thanks.

Bri's picture

Not sure exactly what you are trying to do

Hey there, I am still not quite sure what you are trying to do. The "state" and "partner" drop downs that you are seeing in the first screenshot are just regular filters that have been exposed to the user. You should not need to use a jump menu to create those with taxonomy names. My example was showing how you could create a block that shows a summary of the articles or resources or whatever the content is. When you click one of the block links it does link to a page that uses a contextual filter to show only the items of the type you selected. So, I'm not quite sure which result you are after here. If you want the exposed filters above the page in drop down form, then just expose them on the main page view. If you actually want it in a separate block you can use the exposed filters in block options in views which will put the exposed filters in to a block for you, but they will still control the main page view. I hope I am no misunderstanding what you are after. Maybe i can work up an example tonight.

Anonymous's picture

It'd be easier to use a

It'd be easier to use a taxonomy view with a list of Term: Names, then use the Term:ID as a contextual filter. That way you wouldn't need to do any preprocessing. I suppose you could also do it via a relationship that lets you use Taxonomy Term:Name as a field.

Bri's picture


I think it really depends on your objectives. In most cases people want to show a list of nodes by taxonomy terms, rather than just the terms. While you could still probably get it to work with some custom taxonomy theming, you probably already have the nodes themed the way you want them.

Your second suggestion was one I actually tried to implement and didn't have much success with either. I wasn't getting any results after creating the relationship that allowed me to use the term name as the contextual filter. I may not have had it setup right, i'm not sure.

Anonymous's picture

Hey Bri, I'm a little unclear

Hey Bri,

I'm a little unclear what you mean for the VIEW_NAME and CURRENT_DISPLAY_NAME, how would I find those for the view I am working on? My view is called Example View, and the block I created has not been named anything other than Block.

Bri's picture


If you don't want to use Devel, you can find the view machine name by going and editing it. When you create the view if takes the name you provide and replaces spaces with underscores and makes it all lower case. So a view that you called "Forest Journal" would have a machine name of "forest_journal" and you should be able to see that when you edit the view. As for the display name, the machine name for the display can be found in the "Other" category on the bottom right when you are in a display. You should be able to see that in my screen shots. One of my displays machine names is called "block_1". Hope that helps.

Bri's picture


Hello there,
The view name is the machine name of the view and the display name is the the machine name of the display of the view. Since one view can have multiple displays (a page display and a block display for example) you need to not only make sure you have the right view, but also the right display. The easiest way to see this information and find it would be to enable the Devel module and inside of the function put in a kpr($vars); statement. This will print out all of the vars in a neatly formatted display that will allow you to hide and show sub elements. Good luck!

Anonymous's picture

works perfect, thank you

works perfect, thank you

Anonymous's picture

Add a relationship and use different contextual filter


I've been trying to get a gallery going using taxonomies, views, and some lightbox2 module, and I've actually had this kind of problem. In the process I've found out a more clean solution to this, using just the views without custom code.

First thing you'll need to do is add a relationship on your view - "Content: Taxonomy terms on node". Afterwards, when you are adding a contextual filter you can actually use the term name with the "(term) Taxonomy term: Name". It'll both let you specify a term in the URL and have nicely formatted links output for the summary part (i.e. tags, not numeric ID's).

Branko Majic

Anonymous's picture

Thanks a lot for sharing

I followed your instructions and could get the Taxonomy ID replaced by term names. The only issue I had was that with the recommended contextual filter "(term) Taxonomy term: Name" my taxonomy links in block brought me empty results in clicking on them.

It took me some hours playing around and when trying with "(term) Taxonomy term: Term ID" as contextual filter everything was fine (still setting the relationship as described above). Maybe somebody else will have the same problem, so this could save some investigating time :-)

Bri's picture


Thanks for sharing this!