background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Using taxonomy to create a dynamic layout of nodes grouped by taxonomy term

Eric.London's picture

Taxonomy is a great way of categorizing your content, and in this case we'll take it a step further to organize your content and create a dynamic layout. In a recent situation, we created a taxonomy category for a given node type and assigned 4 terms to the category. The node type contained an image field and a description. We wanted to show a view all the node images, but categorized by taxonomy term. I thought the best way to accomplish this was to get a list of all the taxonomy terms, loop through them, pass the term ID as an argument to a view, and output each view's html dynamically. In the following code snippet, I created a module that generates a block [see below]

When creating your view, make sure you add an argument for "Taxonomy: Term ID". In my example, I selected "Use empty text" for the default argument option to prevent taxonomy terms from being shown if they do not have any matching nodes.

<?php
function MYMODULE_block($op='list', $delta=0) {
 
$block = array();
  switch (
$op) {
    case
'list':
     
$block[0]['info'] = t('MYBLOCKTITLE');
      return
$block;
      break;
    case
'view':
     
$block['subject'] = NULL;
     
$block['content'] = NULL;
     
$block_content = "";
     
     
// define vocab name
     
$vocabName = 'MYCATEGORYNAME';

     
// define view name
     
$viewName = 'MYVIEWNAME';

     
// define node type
     
$nodeType = 'MYNODETYPE';

     
// get vocabularies for this node
     
$vocabs = taxonomy_get_vocabularies($nodeType));     
     
     
// ensure vocabs exist
     
if (count($vocabs)==0 || !$vocabs) return $block;

     
// loop through vocabs and look for matching name
     
foreach ($vocabs as $k => $v) {
        if (
$v->name == $vocabName) {
         
$vocabID = $v->vid;
          break;
        }
      }

     
// ensure vocabID exists
     
if (!$vocabID) return $block;

     
// get vocab term tree
     
$tree = taxonomy_get_tree($vocabID);

     
// loop through tree and collect term IDs
     
$termIDs = array();
      foreach (
$tree as $k => $v) {
       
$termIDs[$v->tid] = $v->name;
      }

     
// loop through term IDs and create views html
     
foreach ($termIDs as $k => $v) {

       
// get view object
       
$view = views_get_view($viewName); 

       
// ensure result is an object
       
if (!is_object($view)) continue;

       
// create view html
       
$viewHtml = views_build_view('block', $view, array($k), FALSE, $view->nodes_per_block);

        if (
strlen($viewHtml)) {
         
// show the taxonomy term name in an <h3>
         
$block_content .= "<h3>$v</h3>";

         
// add the view html to the block
         
$block_content .= $viewHtml;
        }

      }

     
$block['content'] = $block_content;
      return
$block;
      break;
  }
 
}
?>

The result will resemble the following structure...

Taxonomy Term 1
[node] [node] [node]

Taxonomy Term 2
[node] [node] [node]

Taxonomy Term 3
[node] [node] [node]

Using Calais

I am pretty new to Drupal.
If you are using Calais for generating tags, and you want to show the tags in order of their vocabularies, like
Social Tags
[tag 1] [tag2] ...

then how would you do it?

Thanks in advance.