List of taxonomy terms built with Views in Drupal 7

I’ve used the Views module to build a custom block for displaying a list of taxonomy terms (article category tags in this case). It’s working pretty much as I want apart from the lack of the number of results for each category, displayed in brackets, as you get if you use Views to build a monthly archive.
Here’s my exported view, so that you can easily try it for yourself and see what I’ve done so far:

$view = new view;
$view->name = 'categories';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'taxonomy_term_data';
$view->human_name = 'Categories';
$view->core = 7;
$view->api_version = '3.0-alpha1';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Categories';
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'list';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'taxonomy_term_data';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['name']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['name']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = 0;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = 0;
$handler->display->display_options['fields']['name']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['name']['alter']['trim'] = 0;
$handler->display->display_options['fields']['name']['alter']['html'] = 0;
$handler->display->display_options['fields']['name']['hide_empty'] = 0;
$handler->display->display_options['fields']['name']['empty_zero'] = 0;
$handler->display->display_options['fields']['name']['link_to_taxonomy'] = 1;
/* Sort criterion: Taxonomy vocabulary: Vocabulary ID */
$handler->display->display_options['sorts']['vid']['id'] = 'vid';
$handler->display->display_options['sorts']['vid']['table'] = 'taxonomy_vocabulary';
$handler->display->display_options['sorts']['vid']['field'] = 'vid';
/* Sort criterion: Taxonomy term: Name */
$handler->display->display_options['sorts']['name']['id'] = 'name';
$handler->display->display_options['sorts']['name']['table'] = 'taxonomy_term_data';
$handler->display->display_options['sorts']['name']['field'] = 'name';

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');
$handler->display->display_options['block_description'] = 'Category list';

Can views do what I want here? I’m betting that it can, and that I just haven’t figured it out yet.

It’s getting late so I’m calling it a day but there are a few ways to do this. I remember finding some way to do it through views a couple of years ago with D6 but I can’t remember where or how. I have done it with D6 using a views template that is named in such a way that it processes the views results before they get rendered on the page.tpl

what you want to do is create a views template for that specific view you’re working with and modify it to print out whatever you need… It’s a bit tricky and requires some experimentation to make it work. You start with a copy of views-view.tpl.php, rename it and put it in your theme directory with the mods you need to make.

To give you an idea of what it looks like, I have included a D6 views template that I have used to display the number of results. I’ve marked my modification with “MY MOD STARTS HERE”.

Good luck!


// $Id: views-view.tpl.php,v 2010/03/25 20:25:28 merlinofchaos Exp $
 * @file views-view.tpl.php
 * Main view template
 * Variables available:
 * - $classes_array: An array of classes determined in
 *   template_preprocess_views_view(). Default classes are:
 *     .view
 *     .view-[css_name]
 *     .view-id-[view_name]
 *     .view-display-id-[display_name]
 *     .view-dom-id-[dom_id]
 * - $classes: A string version of $classes_array for use in the class attribute
 * - $css_name: A css-safe version of the view name.
 * - $css_class: The user-specified classes names, if any
 * - $header: The view header
 * - $footer: The view footer
 * - $rows: The results of the view query, if any
 * - $empty: The empty text to display if the view is empty
 * - $pager: The pager next/prev links to display, if any
 * - $exposed: Exposed widget form/info to display
 * - $feed_icon: Feed icon to display, if any
 * - $more: A link to view more, if any
 * - $admin_links: A rendered list of administrative links
 * - $admin_links_raw: A list of administrative links suitable for theme('links')
 * @ingroup views_templates
<div class="<?php print $classes; ?>">
  <?php if ($admin_links): ?>
    <div class="views-admin-links views-hide">
      <?php print $admin_links; ?>
  <?php endif; ?>
  <?php if ($header): ?>
    <div class="view-header">
      <?php print $header; ?>
  <?php endif; ?>

  <?php if ($exposed): ?>
    <div class="view-filters">
      <?php print $exposed; ?>
  <?php endif; ?>
  <div id="pre-search-results">
    <h3 class="first-para">Your Search Results:</h3>
      $view = views_get_current_view();
      if($view->total_rows > 0) {
        print "<p>Your search returned " . $view->total_rows . " results.</p>"; 
  <!-- MY MOD ENDS HERE -->  

  <?php if ($attachment_before): ?>
    <div class="attachment attachment-before">
      <?php print $attachment_before; ?>
  <?php endif; ?>

  <?php if ($rows): ?>
    <div class="view-content">
      <?php print $rows; ?>
  <?php elseif ($empty): ?>
    <div class="view-empty">
      <?php print $empty; ?>
  <?php endif; ?>

  <?php if ($pager): ?>
    <?php print $pager; ?>
  <?php endif; ?>

  <?php if ($attachment_after): ?>
    <div class="attachment attachment-after">
      <?php print $attachment_after; ?>
  <?php endif; ?>

  <?php if ($more): ?>
    <?php print $more; ?>
  <?php endif; ?>

  <?php if ($footer): ?>
    <div class="view-footer">
      <?php print $footer; ?>
  <?php endif; ?>

  <?php if ($feed_icon): ?>
    <div class="feed-icon">
      <?php print $feed_icon; ?>
  <?php endif; ?>

</div> <?php /* class view */ ?>

Thanks, Andrew. I’ll look into that.

Why not write a proper, optimized query avoiding the view bottleneck altogether. In most cases it is better to avoid the bottle neck than to use it unless you have absolutely no SQL skills…

I think, oddz, that you answered your own question, albeit in a rather tactless fashion.

I can not understand your writing program,

Ok, that’s possible but where would you insert your query or are you suggesting a one-off module rather than views?