Page templates for specific content types
Wed, 09/12/2009 - 14:05 | by richshaw
Sometimes you come across a situation where you want to display a particular page template for a particular content type. I did this in the image annotation tool I hacked together the other week.
Now the problem is that Drupal doesn't recognise content type based page templates as standard, it's all node based. So Drupal will look the templates in the following order page-node-123.tpl.php < page-node.tpl.php < page.tpl.php.
The solution is to snake an extra page-CONTENT-TYPE.tpl.php into theme registry when a particular content type is loaded.....
<?php
/**
* Add a new template to our node if it's of a certain type
*/
function MODULENAME_preprocess_page(&$variables) {
// If this is a node page (not a list of nodes page) and
// the node is shown in 'view' mode rather than 'edit' or whatever.
// Switch the page template to our custom template.
if (isset($variables['node']) && (arg(2) === NULL) && (arg(0) == 'node') ) {
// If the content type of that one node is 'CONTENT_TYPE_NAME'.
if ($variables['node']->type == 'CONTENT_TYPE_NAME') {
$variables['template_file'] = 'page-CONTENT_TYPE_NAME';
}
}
//Sometimes i.e. when showing nodes in a view. It pulls the page template for the
//nodes rather than the view, I haven't figured out why. To maintain sanity we
//check that our new template isn't set when in shouldn't.
if (in_array("page-CONTENT_TYPE_NAME", $variables['template_files'])
&& !isset($variables['node'])) {
$variables['template_files'][0] = "page";
}
}
?>I think the original code for this came from 11heavens.




