Hi.

I need to be able have a few different layouts (tpl files?) for the same content type in order to display them slightly differently depending on criteria yet to be defined.

Initially I thought I could add a vocab with terms such as : template1, template2, template3... and tag the correct term against the node when I am creating it.

Then somehow do something clever in the template.php file to look for the selected taxonomy term against the node before defining the tpl file to be used to output the content.

However I cannot seem to find how to do this and more than that am not sure that this is even the best way to approach the dilemma.

Has anyone done anything similar or have any better ideas?

Krix.

Comments

nevets’s picture

You could add something like this to your themes template.php file

function YOURTHEME_preprocess_node(&$variables) {
  $node = $variables['node'];
  if ( $node->type == 'YOUR_TYPE' ) {
    // This part depends on your criteria
    // the key is adding to template_files
    if ( some condition is true ) {
      $variables['template_files'][] = 'node-YOUR_TYPE-SOMETHING';
    }
  }
}

I would use node-YOUR_TYPE as the start of the template file and change SOMETHING based on criteria. So lets say I have a content type called bike and a taxonomy that represents the color of the bike I might use a name like 'node-bike-red' and the template file would be node-bike-red.tpl.php

pompeydrupal’s picture

Thanks for the replies, hugely appreciated.

Nevets - I have implemented the solution you proposed by hook_preprocess_node in the template.php and it does exactly what I was looking for.

Thanks again!

davemeyler’s picture

This is exactly what I need to do but I am new to php and drupal so please excuse what might seem obvious.... Can you be more specific on the implementation of this? Meaning what kind of condition? Where to put what, etc. For Example, I have a content type of news with a taxonomy of newsdetail on node x. On the home page I have a region with a view that displays the news content type into a heavily styled layout/node template. When a user clicks on "more" (the more link/href is just the node_url var) I want the landing page/theme to display the same node but formatted differently (like the whole news article, etc). I realize this is exactly what you seem to be explaining but I am not getting the condition part of it. How do I spark the condition based on taxonomy? If I can just get it to call a another node tpl file I know how to tweak the data/vars how i want the formatting to look. It's just getting the preprocess stuff right so it knows what to look for. Thanks so much.

pompeydrupal’s picture

Hey Dave,

Here is the code I implemented in template.php that gave me the functionality I required:

function MYTHEME_preprocess_node(&$variables) {
  $node = $variables['node'];
  if($node->type == 'page') {
    // Check if custom layout defined in taxonomy
    if(!empty($node->taxonomy) && $node->taxonomy[1]->vid == 1) {
      $variables['template_files'][] = 'node-page-' . $node->taxonomy[1]->name;
    }
  }
}

In my setup I have a vocabulary (vid 1 for me) that appears on the page content type. Each term within this vocabulary defines the name of a custom node-page-TERMNAME.tpl.php file.

So the logic breaks down like this:

1) Are we on a page content type?
2) If yes, is there taxonomy associated with this node and if so is it the vocabulary id 1?
3) If yes, this node should be displayed using the 'node-page-TERMNAME.tpl.php' file. TERMNAME being the actual term in the vocabulary.

Hopefully that will help you.

Krix

aiphes’s picture

very interested...but i've a simple question :
when you say "termname" is it the machine name (attribute by drupal) or the label name (type by the term creator) ?

here is my code for template.php :

/*Permet d'attribuer un template de node différent selon taxo et type de contenu
 * - NE PAS OUBLIER DE CHANGER LE NOM DU THEME !!!___
 */
function MYTHEME_preprocess_node(&$variables) {
  $node = $variables['node'];
  if($node->type == 'page_fiche_formation') {
    // Check if custom layout defined in taxonomy
    if(!empty($node->taxonomy) && $node->taxonomy[1]->vid == 1) {
      $variables['template_files'][] = 'node-' . $node->taxonomy[1]->name;
    }
  }
}

and drupal seem not to take node custom template...my VID is 1 like yours, i just change the name of the custom node..
thanks

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

editormp’s picture

I'm not too deep into this, but doesn't the node file have the same capability as the page.tpl file -- i.e. use something like node-template1.tpl.php to get the variations? There is a standard order of searching for the page file. You can use this methof for content type specific layouts

I have a site where the home page content -- including a header -- is based on the taxonomy term and uses only a single page.tpl file. I have tried the same thing by using the specific term as part of the name of the page template.

I'll look further and get back, but someone else may have done this and know more than I do.

(actually, someone else certainly knows more than I do about it)

nevets’s picture

You are correct that system by default support node-TYPE.tpl.php files, they want multiple template files for the same type based on some criteria.

jdln’s picture

Subscribing.