Keep your custom page-forum.tpl.php layout file when viewing a thread

description

This snippet stops phptemplate dropping your custom page-forum.tpl.php layout file when you're viewing a thread.

usage

The way PHPTEMPLATE dynamically loads custom page-layout.tpl.php files in Drupal 5.x is by path, but, when you click through to view a thread the /forum/ part of the path is lost. In other words, you're viewing a node with comments e.g. node/123.

This snippet works around that by adding in an extra check in your template.php file.

Step 1 of 1

  1. Using a text editor like notepad.exe or equivalent, copy the following snippet into your template.php file, remembering to omit the opening and closing PHP tags (i.e. <?php and ?>) if you're adding it to an existing template PHP file.
  2. Upload your new or edited template.php file to your active theme folder and your new layouts will take effect automatically

<?php
/**
* This snippet loads a custom page-forum.tpl.php layout file when
* users click through to the login, request password or register pages
*/

function _phptemplate_variables($hook, $variables = array()) {
  switch (
$hook) {
    case
'page':
  
// node type isn't available in template.php so we have to go get it.
   
if (arg(0) == 'node' && is_numeric(arg(1))) {
     
$node = node_load(arg(1));
     
$type = $node->type;
    }
    if ((
$node->type) == 'forum') {
      
// if the node type is forum load the page-forum.tpl.php layout
       
$variables['template_file'] = 'page-forum';
        }
      break;
  }

  return
$variables;
}
?>

notes

  • Tested with Drupal 5.x (Dublin Drupaller 25th June 2007)
  • If you already have a _phptemplates_variables function and you are not sure how to add this, you can look at this handbook page on how to merge them.
 
 

Drupal is a registered trademark of Dries Buytaert.