How to add a category listing to Drupal

By Hiveminds
Created 2006-05-27 15:50

PHP developer

If you are a Drupal CMS user then you may have noticed that there is no default category listing. When you surf to a link like http://www.drupal.org/taxonomy you get a page not found error. If have noticed this in your own site you may have tried to fix this by adding in a PHP snippet to a block. While adding a block gets you the functionality you need for your site visitors you make your pages heavy with another large SQL query. This is the problem I was having.

It should have been easy enough to fix this by just adding a link to the publicly visible listing for the taxonomy module. But there isn't one. So I went in and created one from another specially brewed taxonomy module. The module is a modified version of the Drupal 4.6 taxonomy file. What I did was take the administration interface and made it website visitor friendly. I used this because the interface is more user friendly than the default listing. Taxonomy can get quite large and I did not want my vistors to be subject to thousands of terms in a enormous table. I then added in a menu callback and linked it. You can see the results here [1].

So far this has proven to be a much better method of giving site visitors a light weight way of surfing by category.I am going to play with this a bit more to see if showing the vocabularies can be made configurable without coding outside of the taxonomy module. I'll make it available later. If you want it sooner then email me from the contact form. Until then here's a bit of code.

<?php

/**
 * Generate a tabular listing of vocabularies.
 */
function taxonomy_page() {
//  $vocabularies = taxonomy_get_vocabularies();
  
$vid arg(2);
  if (!
$vid) {
    
$header = array(t('Name'), t(''), array('data' => t(''), 'colspan' => '3'));
    
$vocabularies taxonomy_get_vocabularies();
    foreach (
$vocabularies as $vocabulary) {
      if (
$vocabulary->module == 'taxonomy') {
        
$types = array();
                    
$child '<div style="display:none" class="voc-node-types" id="'.$vocabulary->name.'-child">';
        foreach (
$vocabulary->nodes as $type) {
          
$node_type node_invoke($type'node_name');
          
$child .= $node_type l($node_type,'node/'.$node_type).'<br />' $type;
        }
                   
$child .='</div>';
        
$rows[] = array(array('data'=>'<strong>'.$vocabulary->name.'</strong>''colspan'=>'5'));
        
// show terms if non-free.
        
if (!$vocabulary->tags) {
          
$tree taxonomy_get_tree($vocabulary->vid);
          if (
$tree) {
                       unset(
$data);
            foreach (
$tree as $number => $term) {
                         
$term_type _taxonomy_depth($term->depth);
                              
$term_edit "taxonomy/term/$term->tid/$term->name";
                              
$zebra = ($number == 1) ? 'dark''light';
                         
$data .= '<tr class="'.$zebra.'"><td>'.$term_type .' '.$term->name.' </td><td> 'l(t('view'), $term_edit,array('rel'=>'tag')) .'</td></tr>';
            }
                        
$rows[] = array(array('data' => '<div class="termslist"><table>'.$data.'</table></div>''colspan' => 5'valign'=>'top'));
          }
          else {
            
$rows[] = array(array('data' => t('No terms available.'), 'colspan' => '5''class' => 'message'));
          }
        }
        elseif (
$vocabulary->tags) {
          
$rows[] = array(array('data' => t('This is a free tagging vocabulary:') . ' ' l(t('view terms'), "taxonomy/tags/$vocabulary->vid") . '.''colspan' => '5''class' => 'message'));
        }
/** this is to cover vocabularies that should not be freetagged like Forum and others where the taxonomy
    effects the structure of the output
*/
}else{
    
$rows[] = array(array('data'=>'<strong>'.$vocabulary->name.'</strong>','colspan'=>'5'));
    
$tree taxonomy_get_tree($vocabulary->vid);
    if (
$tree) {
       unset(
$data);
       foreach (
$tree as $number => $term) {
             
/* check to see if the term used is a forum container before listing so that the proper form can be shown on edit */
             
if ($term->tid && in_array($term->tidvariable_get('forum_containers', array()))) {
              
$term_type '[ forum container ]';
              
$term_edit "forum/container/$term->tid";
             }else{
                 
$term_type _taxonomy_depth($term->depth);
                 
$term_edit "taxonomy/term/$term->tid/$term->name";
             }
           
$zebra = ($number == 1) ? 'dark''light';
        
$data .= '<tr class="'.$zebra.'"><td>'.$term_type .' '.$term->name.' </td><td> 'l(t('view'), $term_edit,array('rel'=>'tag')) .'</td></tr>';
       }
        
$rows[] = array(array('data' => '<div class="termslist"><table>'.$data.'</table></div>''colspan' => 5'valign'=>'top'));
    }else {
          
$rows[] = array(array('data' => t('No terms available.'), 'colspan' => '5''class' => 'message'));
    }
}
    }
    if (!
$rows) {
      
$rows[] = array(array('data' => t('No categories available.'), 'colspan' => '5''class' => 'message'));
    }
  }
//  if (!$rows) {
//   $rows[] = array(array('data' => t('No categories available.'), 'colspan' => '5'));
  // free tagging vocabularies get their own terms pager
  // since there is a greater chance of 1000+ terms.
  
else {
    
$header = array(t('Name'), array('data' => t('Operations'), 'width' => '60'));
    
$vocabulary taxonomy_get_vocabulary($vid);
    if (
$vocabulary->module == 'taxonomy') {
      
drupal_set_title($vocabulary->name);
      
$start_from      $_GET['from'] ? $_GET['from'] : 0;
      
$total_entries   0;  // total count for pager.
      
$page_increment  25// number of tids per page.
      
$displayed_count 0;  // number of tids shown.
      
$tree taxonomy_get_tree($vocabulary->vid);
      foreach (
$tree as $term) {
        
$total_entries++; // we're counting all-totals, not displayed.
        
if (($start_from && $start_from $total_entries) || ($displayed_count == $page_increment)) { continue; }
        
$rows[] = array(_taxonomy_depth($term->depth) . ' ' $term->namel(t('view'), "taxonomy/term/$term->tid"));
        
$displayed_count++; // we're counting tids displayed.
      
}
      if (!
$rows) {
        
$rows[] = array(array('data' => t('No terms available.'), 'colspan' => '2'));
      }
      
$GLOBALS['pager_from_array'][] = $start_from;
      
$GLOBALS['pager_total'][]      = $total_entries;
      
$rows[] = array(array('data' => theme('pager'NULL$page_increment), 'colspan' => '2'));
    }
  }
//  return theme('table', $header, $rows);
$output theme('table'$header$rows, array('id' => 'taxonomy')).'';
  print 
theme('page'$output);
}
?>

Source URL: http://www.windowscomputer.com/content/how-to-add-a-category-listing-to-drupal.html