Archive for April 6th, 2009

Drupal: Combining Taxonomy Menu and Taxonomy Redirect

Taxonomy Menu is a very nice Drupal module that lets you create a menu based on a content type’s taxonomy. The taxonomy terms form the menu structure, and the way the nodes are displayed when a menu item (= taxonomy term) is selected can be customised using a custom view. I really like it. I like it so much, that I wanted the taxonomy terms displayed in the header of my nodes to link to the same menu structure, so that clicking on them was the same as clicking on the same term in the Taxonomy Menu.

Here enters Taxonomy Redirect. This module allows one to rewrite the url that the Taxonomy module creates. So instead of blabla.com/taxonomy/term/999 you can make the link blablabla.com/goto/this/instead/999. For this, you can define a redirect as plain text, or with a php snippet. Due to the link structure Taxonomy Menu uses, I had to resort to php for dynamically creating the Redirect link. The standard format for the link structure for Taxonomy Menu is “sitename.com/category/vid/pid1/pid2/tid” (with vid the vocabulary id, pid a parent term id, and tid the term id). The term “category” can be set in the settings for Taxonomy Menuy. Using plain text to define the Redirect link would f*** things up when the taxonomy term did not have any parents. Php worked fine, once I found out that the Redirect module defines a variable $tid for the term id, that you can use in your script. The code I used:

<?php
$output = “category/1/”;
$parents = taxonomy_get_parents_all($tid);
$parents = array_reverse($parents);
// Remove the child term from the array
array_pop($parents);
foreach ($parents as $parent) {
$output .= “$parent->tid”;
$output .= “/”;
}
$output .= $tid;
return $output;
?>

The “1″ behind “category” in the first line is for the vocabulary, so you’ll have to check what value you need there. Note that using php might not work straight away for anonymous users. To resolve this, have a look here