I'm upgrading a site of mine to 6.x, and had used a code snippet which worked well by creating a function in template.php called by a variable in node.tpl.php. This injected the terms into the class of each node, allowing me to theme them via css.

The snippet is broken in 6.x so I did a bit of research...but I'm not really a programmer so I could use some help. Here's what I found:

  • _phptemplate_variables() is depreciated, so it will use theme preprocessors which I'm beginning to understand, just don't know enough php to recode the old snippet.
  • Also, taxonomy_node_get_terms has changed and again, I can't figure out how to rewrite.

I'd appreciate some help on this, and would love to update the snippets page

Comments

francort’s picture

Taxonomy theme module isn't available for drupal 6 yet :(
But in the meantime maybe this thread and this thread can help you a bit

michaeldhart’s picture

Thanks, francort, I actually tried the dev version of Taxonomy Theme for 6, but it wants to apply an entirely different theme to a particular node or page. Whereas, all I want to do is allow for taxonomy terms to become css classes so I can apply a style based on which term the node has. If it helps, here is the code I'm trying to convert to 6 (found it here):

<?php
function _phptemplate_variables($hook, $vars) {
  if (
$hook == 'page') {
   
$prefix = ' b-tax-';
  }
  if (
$hook == 'node') {
   
$prefix = ' tax-';
  }
  if (
module_exists('taxonomy') && $vars['node']->nid) {
    foreach (
taxonomy_node_get_terms($vars['node']->nid) as $term) {
     
$vars['node_terms'] = $vars['node_terms'] . $prefix . eregi_replace('[^a-z0-9]', '-', $term->name);
    }
  }
  return
$vars;
}
?>

...which goes into the template.php and:
class="<?php print $node_terms; ?>"
...which goes into both the body tag of page.tpl.php and node.tpl.php

Does that make sense?

michaeldhart’s picture

So, hackin' around a bit, I got working what I wanted to. thought I'd post it in case anyone is interested. I modified the code above, but only wanted to modify the class of a node, not a page, so I got rid of the extra code pertaining to styling a page and just kept the node stuff:

<?php
function themeName_preprocess_node(&$variables) {
    if (
module_exists('taxonomy') && $variables['node']->nid) {
    foreach (
taxonomy_node_get_terms($variables['node']) as $term) {
     
$variables['node_terms'] = $variables['node_terms'] . ' tax-' . eregi_replace('[^a-z0-9]', '-', $term->name);
    }
  }
}
?>

Put this in your template.php file and then this:

class="<?php print $node_terms; ?>"

in the node.tpl.php file.

Summit’s picture

Subscribing, greetings, Martijn

iamwhoiam’s picture

To get it working on the page (in body)

<?php
function phptemplate_preprocess_page(&$vars) {
    if (
module_exists('taxonomy') && $vars['node']->nid) {
    foreach (
taxonomy_node_get_terms($vars['node']) as $term) {
     
$vars['node_terms'] = $vars['node_terms'] . ' pagetax-' . eregi_replace('[^a-z0-9]', '-', $term->name);
    }
  }
}
?>

Simply preprocess_page

joerhoney’s picture

If you're using a Zen subtheme, you'll have to uncomment the override function that applies to pages or nodes (whichever you're trying to use this snippet for). The instructions in the file explain which function overrides what and which line to delete to uncomment it.

Then rebuild the site registry in order for the changes to take effect.

This is just something I ran into, so I'm sharing it here.

gavjof’s picture

Could you be more specific?
I've tried adding the above into override function of /zen/subtheme/template.php without success.

Here is where I added it, around line 130.

<?php
/* -- Delete this line if you want to use this function
function STARTERKIT_preprocess_node(&$vars, $hook) {
  $vars['sample_variable'] = t('Lorem ipsum.');
}
// */
?>

but when I load up my site I simply get a blank page. I'm guessing it's conflicting with the other zen_preprocess_node function in the main zen theme?