Overriding Theme Templates in Drupal 6

Last modified: August 27, 2009 - 02:25

Adding additional preprocess functions inside existing preprocess functions

You may want to add additional preprocess functions inside your themes preprocess function or preprocess_hook function to create different variable in different situations. For instance, if you wanted to have a node-type template (node-story.tpl.php) you could create a function called MYTHEME_preprocess_node_story(), where you would set story specific variables. The title of the function is not called trough the theme API, so you could call it whatever you like, but this would allow you to setup your own API for calling all node-type templates with only a few lines of code.

<?php

function MYTHEME_preprocess_node(&$vars, $hook) {
 
// Variables available to every node are defined here

  // Now define node type-specific variables by calling their own preprocess functions (if they exist)
 
$function = 'MYTHEME_preprocess_node'.'_'. $vars['node']->type;
  if (
function_exists($function)) {
  
$function(&$vars);
  }
}
?>

Hi, I am trying to upgrade my

leenyx - April 19, 2009 - 21:00

Hi, I am trying to upgrade my website from 5x to 6x but I suppose that there is a much steep learning curve with Drupal 6x. I can make this work in my website. I would like to have custom templates for each as I had with the 5x version. For instance have a static page called page-node-1.tpl.php and another one called page-nodetype-portfolio.tpl.php.

So far I can make work one or the other but I can't put the two together, and the examples here are not much helpful either as explaining things...

Is anyone around being able to work this out? please can you post a more helpful example.

Thanks

found it...

leenyx - April 19, 2009 - 21:19

I guess you have it all guys well explained after all...

function YOURTHEME_preprocess_page(&$variables) {
global $user;
if ($variables['node']->type == "YOUR_NODETYPE") {
$variables['template_files'][] = 'page-node-YOUR_NODETYPE';
}
else if (module_exists('path')) {
//allow template suggestions based on url paths.
$alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
if ($alias != $_GET['q']) {
$suggestions = array();
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$suggestions[] = $template_filename;
}
$alias_array = explode('/', $alias);
$variables['template_files'] = $suggestions;
}

// Add a single suggestion.
if (module_invoke('throttle', 'status') && isset($user->roles[1])) {
$variables['template_file'] = 'page-busy';
}

// Add multiple suggestions.
if (!empty($user->roles)) {
foreach ($user->roles as $role) {
$filter = '![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s';
$string_clean = preg_replace($filter, '-', drupal_strtolower($role));
$variables['template_files'][] = 'page-'. $string_clean;
}
if (drupal_is_front_page()) {
$variables['template_file'] = 'page-front';
}
}
}
}

from http://drupal.org/node/223440

That works for me, thanks

Thank you for this

checkmark - August 19, 2009 - 22:58

For the longest time I thought MYTHEME_preprocess_node_story() WOULD be called by the theme API in template.php. This code is exactly what I needed to make it work. Thank you very much!

Could someone please refer me to precisely the page that explains exactly which preprocess functions the theme API DOES search for? I'm confused about which .tpl.php files you can preprocess for directly (like for node.tpl.php) versus which template files you CANNOT preprocess for (like node-story.tpl.php, unless you use the code given above). Is the issue that node-story.tpl.php is a "suggested" template? Or am I misunderstanding something?

newest versions of php

mansspams - September 30, 2009 - 12:01

this snipet broke views ajax. this line was faulty "$function(&$vars);". i changed to "$function($vars);" and it was all right. Am I doing it right?

Yes. Call time pass by

adamo - October 1, 2009 - 15:27

Yes. Call time pass by reference is deprecated in newer versions of PHP.

 
 

Drupal is a registered trademark of Dries Buytaert.