Hello, is there a way (built-in or otherwise) to add preprocessing functions for particular cck node types? I am looking to do some preprocessing of a field within my cck node type. Currently I can either use theme_preprocess_node and then do a switch on the $node->type or use a theming function for a particular field name (and still do a switch to make sure the current field usage is within the node type i'm looking for). What I am suggesting is to have a function like this...

theme_preprocess_mynodetype(&$vars) {
// Now I can preprocess a field without testing whether the field is within the target content type
}

...but I can't figure out if I can suggest preprocess functions the same way I can suggest template files

Thanks! Rob

Comments

eojthebrave’s picture

I usually add something like this to my template.php file for my theme.

/**
 * Override or insert variables into the node templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("node" in this case.)
 */
function mytheme_preprocess_node(&$vars, $hook) {
  // Optionally, run node-type-specific preprocess functions, like
  // threesixty_d6_preprocess_node_page() or threesixty_d6_preprocess_node_story().
  $function = __FUNCTION__ . '_' . $vars['node']->type;
  if (function_exists($function)) {
    $function($vars);
  }
}

Which then lets me have preprocess functions like:

function mytheme_preprocess_node_article(&$vars) {
  // Article type specific preprocessing code.
}

function mytheme_preprocess_node_event(&$vars) {
  // Event type specific preprocessing code.
}
rob5408’s picture

Nice one, I'll give that a shot, thanks! rob

jardineworks’s picture

I'm trying to use this function to do the same thing as you have listed above. I included it in my template.php file and have even printed the $function variable to make sure that I have the right function names.

I load the page and am able to see that the theme_preprocess_node that I defined was called, and I see the function name it's supposed to call listed. For some reason though the variables that I added to the vars array are not appearing on the page.

... am I missing something?

eojthebrave’s picture

Make sure you're passing your variable by reference.

So, make sure your function signature is as follows.

function myfunction_preprocess(&$vars) {

The "&" is essential here.

If that's not it could you paste your code so we can take a look and see if there is anything obvious missing.

jardineworks’s picture

What's more important is that I spell process with two 's' ... not one. Works like a charm.

Thanks!

firebus’s picture

awesome.

13rac1’s picture

Here is the D7 version. Extra underscore to follow conventions.

function theme_preprocess_node(&$variables) {
  $function = __FUNCTION__ . '__' . $variables['node']->type;
  if (function_exists($function)) {
    $function($variables);
  }
}
wylbur’s picture

Instead of creating a new function for each content type. Why not use if statements within the variables preprocess function to control the action for each content type. It's easy to read, and you can just add new sections when you add new content types.

<?php
function mytheme_preprocess_node (&$vars) {

/* Variables for all nodes. */
$node = $vars['node'];
...
...

/* Variables for Connect Articles */
if ($vars['type'] == "connect_article"):
...
...
endif;

}
?>