Tuesday, April 24, 2018 04:29 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.