Last updated 25 August 2009. Created on 18 February 2007.
Edited by bekasu, mpare. Log in to edit this page.

Description

This function was designed to accomplish two purposes. 1) to allow block visibility only on nodes who have a root term that matches function input. 2) to allow for css class output to be used to theme different portions of the site. This function has many uses beyond those two but those were my original intent.

Step 1 of 2

copy the below code into your template.php. You should be using this only on phptemplate themes. If you don't know if you have a phptemplate theme look inside your current themese directory. If any of the files end with .tpl.php you are using a phptemplate theme.
You can optionally change
<?php function phptemplate_show_menu($match) ?>
to
<?php function <yourtemplatesname>_show_menu($match) ?>
if you want this function to only work with your current theme.

<?php

/**
 * 02/18/07
 * Created by: Matthew Pare - Pare Technologies
 * http://www.paretech.com
 *
 * This function was designed to accomplish two purposes. 1) to allow block
 * visibility only on nodes who have a root term that matches function input.
 * 2) to allow for css class output to be used to theme different portions of
 * the site. This function has many uses beyond those two but those were my
 * original intent.
 *
 * You may freely use, distripute and edit as desired
 *
 * Originally posted at http://drupal.org/node/120419
 */
function phptemplate_show_menu($match) {
  // Checks to make sure we are viewing a node
  if ( arg(0) == 'node' && is_numeric(arg(1)) && !$in_preview ) {
    $node = node_load(arg(1)); // is cached
    // fetches the taxonomy array from the node object
    foreach ($node->taxonomy as $tax) {
      // retrieves all the term id's from the taxonomy array and gets the parent
      foreach (taxonomy_get_parents_all($tax->tid) as $parent) {
        //stores the term names only as an array
        $vocabs[] = check_plain($parent->name);
      }
    // checks to see if node is assigned to the desired term and returns true
    if ($match && in_array($match, $vocabs)) {
      return TRUE;
    } elseif (!$match) {
        // if you choose not to provide an input a css class attribute is the output
        $cssclass = strtolower(implode(" ", (str_replace(' ', '', $vocabs))));
        return $cssclass;
      }
    }
  }
}

?>

Usage 1 of 2

If you are wanting this snippet to control block visibility, customize your block. Go down to "Page specific visibility settings" and select "Show if the following PHP code returns TRUE (PHP-mode, experts only)."
Then, in the "pages:" text field enter
<?php phptemplate_show_menu(<yourdesiredtermtomatch>) ?>
or
<?php <yourtemplatesname>_show_menu(<yourdesiredtermtomatch>) ?>
if you chose to go that route.
Make sure you change , without < >, to be the name of your term you wish the block to appear on.

Usage 2 of 2

You can alternatively put the function directly in your theme. When you do this enter
<?php phptemplate_show_menu() ?>
or
<?php <yourtemplatesname>_show_menu() ?>

When nothing is in () the function will then pull all the terms in your heirarchy, seperated by a space.
As an example, say that the node you are viewing is assigned to term2, term2 is a child term of term1. Using the function with nothing in the () would return term2 term1.
This then allows you to hook on css with .term2.term1 {} to apply your styling.

Notes

  • This snippet was built and tested on Drupal 5.x, though it may work in previous versions. Please comment it if does
  • This function is not limited to the above tasks, many other options and usage exist
  • If you find errors please let me know and I will correct

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.