Thursday, May 21, 2015

Drupal template redirect

I have faced one challenge recently where we need to redirect from one custom node template to another one template.
  • Created one template for custom content type (nature).
  • node--nature.tpl.php
  • node--my_nature.tpl.php (if your content type - my_nature: some time you will face this situation.)
  • This content type has one taxonomy reference.
  • For particular taxonomy reference(condition), we need to display different appearance (layout).
  • So done below fix to redirect for different tpl.
When ever you visit nature content, you will be landed on node--nature.tpl.php.
Inside this tpl, I have used below condition to use different tpl file.

// Write below code inside node--natute.tpl.php on top of the file.
if (condition) {
 print theme('use_different_layout', array('node' => $node)); // pass argument if you need.
// Create different tpl file for "use_different_layout" theme by using hook_theme().

hook_theme() example.
function hook_theme() {
return array(
    'use_different_layout' => array(
      'variables' => array('node' => array()),
      'template' => 'templates/different-layout.tpl.php',

No comments:

Post a Comment