Community Documentation

Overriding Theme Templates in Drupal 6

Last updated August 27, 2009. Created by Todd Nienkerk on November 20, 2008.
Edited by bekasu, frankcarey. Log in to edit this page.

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);
  }
}
?>

Comments

Hi, I am trying to upgrade my

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...

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

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

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

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

MYTHEME_preprocess_node(&$var

MYTHEME_preprocess_node(&$vars, $hook) worked well for me but MYTHEME_preprocess_page(&$vars) for this node didn't add new variables to the templates

Concatenating of the

Concatenating of the underscore is a bit weird.

<?php
  $function
= 'MYTHEME_preprocess_node'.'_'. $vars['node']->type;
?>

Should be:
<?php
  $function
= 'MYTHEME_preprocess_node_'. $vars['node']->type;
?>

Or, as some people find it a bit more readable:
<?php
  $function
= "MYTHEME_preprocess_node_{$vars['node']->type}";
?>

This is also working with

This is also working with Drupal7

Luis

About this page

Drupal version
Drupal 6.x
Audience
Themers

Theming Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here