1

I am overriding node.tpl.php for a specific node (node--news.tpl.php) to display it differently in the front page. Instead of copying node.tpl.php code into node-news.tpl.php to display the node with the default rendering when we are not on the front page, I would rather prefer calling the base template implementation (node.tpl.php).

if ($is_front)
  // ...
else
  base_template(/* ... */);

Is that possible ?

Thanks,

Solution :

function template_preprocess_node(&$variables)
{
  if (drupal_is_front_page() && $variables['node']->type == 'news')
    $variables['theme_hook_suggestions'][] = 'node__news__front';
}

Then, simply edit the template file node--news--front.tpl.php

1

I would do this in the preprocess stage, eg, in your preprocess_node() hook, do something along the lines of:

$node=$variables['node'];
if ($_GET['q']=='node') {                           // eg the frontpage (by default anyway) and not node/## (also then presumes this is a teaser view, too
  $foo=drupal_get_path_alias('node/' . $node->nid;  // or perhaps $foo=$node->path; works in D7?
  if ($foo=='news') {
    $variables['theme_hook_suggestions'][]='node__news'; 
  }
}

I saw "along the lines of" because I am just now getting to know D7 vs D6 and I could have some typos in there, but the logic is to set the template file to the node-news.tpl.php one only when the user is looking at the front page and your news node is presented there.

But also, from what I have seen, node--news.tpl.php suggests that it should be used for nodes of TYPE news, so, there may need to be a different name here as well. So, if that's what you mean, you could do this instead:

$node=$variables['node'];
if ($_GET['q']=='node' && $node->type=='news') {
  $variables['theme_hook_suggestions'][]='node__news__front';
}

and create a node--news--front.tpl.php file, or whatever the best practices name is :)

1
  • It works, thank you :) Drupal could (should) nevertheless offer a better (faster) solution. – Julio Guerra Mar 5 '12 at 17:18
1

I have never tried this, but I suspect you can implement template_preprocess_node in your theme, use drupal_is_front_page, and if FALSE, remove the specific template from the $variables["theme_hook_suggestions"] array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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