Drupal 5: Creating a Block View that shows Related Content using Taxonomy and Arguments

Here is how you can create a block view that shows related content items based on taxonomy. First, setup taxonomy for your node types. In my example, I created a category named ‘Topics’ and added a bunch of terms to it. Next, I edit/added my nodes and assigned taxonomy to them of this category.

Second, create a block view. Make sure you enter all the necessary information (filters, fields, sort criteria, etc). The piece that makes this view work is the argument. Add an argument of type “Taxonomy: Term ID”. Change the option to “use empty text”. I then assigned this block to a region and set it to only show up on certain node pages.

The next part involves adding some code. In my example, I needed to modify the html/css/layout of the block, so I decided to add a file to my theme called “block-views-MYVIEWNAME.tpl.php”. This could have been accomplished using argument handling code (in the view settings), but I prefer to keep my code out of my database when possible, to version my code using subversion. Here’s the contents of the block-views-MYVIEWNAME.tpl.php file:


// get node id from query string
if (substr($_REQUEST['q'], 0, 5)=='node/') {
  $nodeID = substr($_REQUEST['q'], 5);
} else {
  $nodeID = substr(drupal_lookup_path('source', $_REQUEST['q']), 5);

// ensure the nodeID exists
if (!is_numeric($nodeID) || !$nodeID) return;

// load node object
$node = node_load($nodeID);

// ensure the node object was loaded
if (!is_object($node)) return;

// get vocabularies for this node
$vocabs = taxonomy_get_vocabularies($node->type);

// ensure there is taxonomy
if (!is_array($vocabs)) return;
if (count($vocabs)==0) return;

// loop through vocablularies, look for vid that matches vocab name
foreach ($vocabs as $vocab) {
  if ($vocab->name == 'Topics') {
    $vid = $vocab->vid;

// lookup terms for this node, by vocabularyID
$nodeTerms = taxonomy_node_get_terms_by_vocabulary($node->nid, $vid);

// loop through node terms and get a list of term IDs
$nodeTermIDs = array();
foreach ($nodeTerms as $nodeTerm) {
  $nodeTermIDs[] = $nodeTerm->tid;

$args = $nodeTermIDs;

if (count($args)==0) return;

// get view object
$view = views_get_view('related_content');

if (!is_object($view)) return;

// generate view html
$viewHtml = views_build_view('block', $view, $args, FALSE, $view->nodes_per_block);

if ($view->block_title && $viewHtml) echo "<h2>" . $view->block_title . "</h2>";
echo $viewHtml;