Adding new variables to a specific node type

Drupal: 13m
Video Series
Information

We'll go a step further with our preprocess functions and look at working with node variables, and how to limit new variables to only specific content types.

Discussion

Comments on this video

You need to be an active subscriber to comment. or .

I noticed in this tutorial we were targeting article node types. So I'm assuming we could have written

function ninesixtyrobots_preprocess_node_article(&$variables){

I'm just wondering if this is true and why not do it this way?

Thanks

For preprocess functions in a theme, you can only name the function using the base name of the template like 'page' or 'node' where page/node is actually the hook. See http://api.drupal.org/api/drupal/includes--theme.inc/function/theme/7

However, you may add a new function in template.php to allow for the desired functionality of also including the content type in the function name:


function THEMNAME_preprocess_node(&$variables) {
$function = 'THEMENAME_preprocess_node_' . $variables['node']->type;
if (function_exists($function)) {
$function($variables);
}
}

Lullabot logo

Lullabot has trained thousands of Drupal developers & guided the development of some of the largest Drupal websites.