2

I would like to change the tpl.php file used depending on the taxonomy term applied to a node.

There is a snippet on this page that explains how to do that. However, it is written for Drupal 6, and doesn't work for Drupal 7.

The snippet is as follows:

// Add this to your theme's template.php
// Replace "THEMENAME" with your theme's name
function THEMENAME_preprocess_node(&$vars) {
  foreach ($vars['node']->taxonomy as $term) {
    $vars['template_files'][] = 'node-term-'. $term->tid;
  }
}

I have tried to update it for Drupal 7 as follows:

function THEMENAME_preprocess_node(&$variables) {
  foreach ($variables['node']->taxonomy as $term) {
    $variables['template_files'][] = 'node-term-'. $term->tid;
  }
}

However, it still doesn't work. I just get the following error message:

Notice: Undefined property: stdClass::$taxonomy in THEMENAME_preprocess_node() Warning: Invalid argument supplied for foreach() in THEMENAME_preprocess_node()

Any help would be appreciated.

3 Answers 3

4

You could probably handle it with the following code:

function mytheme_preprocess_node(&$variables) {
  if (isset($variables['node']->field_myfield[$variables['node']->language][0]['tid'])) {
    $tid = $variables['node']->field_myfield[$variables['node']->language][0]['tid'];
    $variables['theme_hook_suggestions'][] = 'node__term_'. $tid;
  }
}

Now if a term with the id 6 would be referenced from a node's 'field_myfield' Drupal would look for a template named 'node--term_6.tpl.php' and use that if available.

0
2

Here you can find everything you need to know about template suggestion in D7.

I think in your case, the template should look like page--taxonomy--term--1.tpl.php. No need for preprocess functions.

1
  • 1
    Thanks! But page--taxonomy--term--1.tpl.php changes the page.tpl.php file. I want to change the node.tpl.php file. How can I do that (The page you linked to doesn't explain it).
    – big_smile
    Feb 27, 2012 at 13:01
2

Try this instead:

function THEMENAME_preprocess_node(&$variables) {
  foreach ($variables['node']->taxonomy as $term) {
    $variables['theme_hook_suggestions'][] = 'node-term-'. $term->tid;
  }
}
4
  • Hi - Thanks! That code produces an error message. (It's the same error message as listed in my opening post). Thanks for taking the time to help!
    – big_smile
    Feb 27, 2012 at 15:29
  • you should var_dump the $variables['node'] and see the exact structure of the variable. I think in D7 the node structure is changed regarding the taxonomy. Locate the taxonomy related part in $variables['node'] and make the necessary adjustments Feb 27, 2012 at 15:45
  • How do I do that? Also what would I look for to make the necessary adjustments?
    – big_smile
    Feb 27, 2012 at 15:56
  • put this before foreach(... and tell me what it'll show up: drupal_set_message('<pre>' . print_r($variables['node'], true) . '</pre>'); Feb 27, 2012 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.