Working with Drupal again today, getting ready to push out “version 2″ of a particular site I have been working on this year and I ran into a bit of a snag. I found something that needed cleaning, I knew what needed to be done, just wasn’t sure how to do it. As anybody who has had the pleasure of working with Drupal you know that there is a ton of info out there than can teach you just about anything you need to know. Problem is, or at least what I have found to be a problem, is that it is not always extremely easy to find. So I’m sharing this snippet here because it took forever for me to dig up. Basically this snippet of code, when inserted into your themes template.php file, will allow you to create a node.tpl.php based on a taxonomy term.
So for instance…
On a particular site that I am working with right now I was hoping to create a node template file that would allow me to style both the teaser and the main page in a slightly different manner than I had for all of the others site-wide. Nothing to exciting really it had to do with displaying quotes in a certain way. I knew that there had to be a way to implement this using template.php but being somewhat new at drupal I wasn’t exactly sure of the bat.
Needless to say, after about 3 hours of search drupal.org as well as the internet in general I finally found this snippet on drupal.org contributed by grendzy. “preprocess_node” of course that makes a lot of sense. Basically what this does is makes a node template such as node-term-14.tpl.php available for use, term 14 being the number of your taxonomy term.
<?php
// Add this to your theme's template.php
// Replace "garland" with your theme's name
function garland_preprocess_node(&$vars) {
foreach ($vars['node']->taxonomy as $term) {
$vars['template_files'][] = 'node-term-'. $term->tid;
}
}
?>
Unfortunately there was not a whole lot of feedback on this particular thread so I can not say for sure that this will work for everyone, nor can I say for sure that it will be error free. I can say that it has worked perfectly for my situation and for that I am thankful.
Super-helpful. Exactly what I needed. Thanks!
Great, glad I could help. It definitely took me some time to hunt this one down. While I don’t take credit for the snippet I thought it would be helpful to share and link out to the source in hopes that it might help someone find it a bit quicker. As always things change in open-source so it definitely doesn’t hurt to do some research/testing to be sure this is current.
Thanks for the comment!
Hey Jeremy, thanks for all your work. I was wondering what version of drupal you were using with this snippet?
This function is exactly what i’m looking for but i get drupal’s white screen of death when using this snippet in my template.php file. I tried it in both Garland and a custom built theme. the version of drupal i did the testing is 6.16.
any experience with this concept lately?
Thanks again.
Hey Casey – I can tell you for certain that is was pre Drupal 6 and about as long since I actually dug into a Drupal powered site.
Perhaps I will do some research on this and get back to you on it. In the mean time if you happen to find a new solution for how you accomplish this post Drupal 6, please feel free to drop by and leave it in the comments.
the output of this code can be found here: http://bit.ly/bQ2pAx multiple layouts within one node.tpl.php file base on taxonomy.
No worries, only spam if it includes Viagra links. Thanks for trying to post up the new code, you might have better luck trying to add it to http://pastie.org and then linking to it from the comments. If you would like to of course. I am not sure how much help I would be, Drupal is definitely not one of my strong points and obviously you know what you are doing, but I would be interested to check it out.
good idea. here is my complete node.tpl.php file: http://bit.ly/c1aUf1
The layout is based on a custom css grid design from my designer.
I would like to learn how to put this in template.php and eventually a module.