Drupal Preprocess & Process Functions

Need to override something in Drupal? Welcome to Drupal preprocess and process functions — your variable override headquarters. Here’s a simple explanation of what they are and how to use them.

Drupal

The main role of the Drupal preprocessors is to set up variables to be placed within the template (.tpl.php) files. In Drupal 7, they apply to both templates and functions. The preprocessing hook provides frontend developers more control over the output allowing for more clean, efficient code. With a little basic PHP knowledge, after having read this post. Drupal preprocess and process functions will by crystal clear.

What are Drupal preprocessors?

By definition, “preprocess” is a phase of processing that happens before templates are rendered. “Process” functions, serve the same purpose, with the only difference being that they run later (after preprocess) in the processing cycle — it’ll make sense later on in this post.

I recommend having some knowledge of template files. If not then please visit drupal.org or Goooogle it. Drupal core and contributed modules produces output in template files. These templates usually have HTML markup & PHP variables — the variables you’re probably trying to override.

Drupal Preprocess & Process Functions
The image above is a basic illustration of Drupal template files.

Drupal’s Preprocess Functions

Now let’s look at Drupal’s template_preprocess() function. This function is the default implementation of preprocess by Drupal and is the first preprocess function called. Here the $classes_array variable is initialized, refer to http://api.drupal.org/api/function/template_preprocess/7.

Excerpt from Drupal’s template_preprocess function where $classes_array is defined:

function template_preprocess(&$variables, $hook) {
  // Initialize html class attribute for the current hook.
  $variables['classes_array'] = array(drupal_html_class($hook));
}

Starting to get confused? No worries, so did I. Let’s break down the code above. The function provides two variables, $variables and $hook. Per Drupal’s API, the template_preprocess() function is called for theme hooks implemented as templates only, not for theme hooks implemented as functions. This preprocess function is the first in the sequence of preprocessing and processing functions that is called when preparing variables for a template.

The first step adds a class to the classes array indicating which hook is being used. For example, let’s say the preprocess function is being called for a node. The code above will add the class node to the array. Once this function runs, all enabled modules and themes will also have a change to run their own template_preprocess() functions to add or change any of the variables.

Now let’s look at the template_proprocess_node(), refer to http://api.drupal.org/api/function/template_preprocess_node/7.

function template_preprocess_node(&$variables) {
  // Gather node classes.
  $variables['classes_array'][] = drupal_html_class('node-' . $node->type);
  if ($variables['promote']) {
    $variables['classes_array'][] = 'node-promoted';
  }
  if ($variables['sticky']) {
    $variables['classes_array'][] = 'node-sticky';
  }
  if (!$variables['status']) {
    $variables['classes_array'][] = 'node-unpublished';
  }
  if ($variables['teaser']) {
    $variables['classes_array'][] = 'node-teaser';
  }
  if (isset($variables['preview'])) {
    $variables['classes_array'][] = 'node-preview';
  }
}

This shouldn’t look to confusing after reading about the template_preprocess() function. It does the same thing, but at the node level. Just like the template_preprocess() function, once ran, all enabled modules and themes will also have a chance to implement their own version.

Drupal’s Process Functions

After Drupal has completed running all of the preprocess functions, the process functions are up next. Drupal’s core, by default only has two process functions for nodes. Those are the template_process() and the rdf_process() functions. Let’s take a look at the template_process() as an example of process functions.

function template_process(&$variables, $hook) {
  // Flatten out classes.
  $variables['classes'] = implode(' ', $variables['classes_array']);
}

After all of the modules and themes have ran their preprocess functions, the template_process() function created a new variable called classes. This variable has a string version of all the classes that were defined in the classes_array. It get’s printed in the class attribute of the div wrapper in the node.tpl.php.

In Conclusion

You should now have a basic understanding of what Drupal preprocess process functions are and the order in which they get ran. Their powerful features allows you the flexibility and power front-ender’s need to code efficient and compliant code. The important thing to take away from this article is that on the theme layer, you’ve got the last call on these variables. There you can add, change, and remove any variables you please by using the appropriate preprocess and process functions.

Bottom line here is when using preprocess and process functions, you keep your code nicely separated. The preprocess and process functions are the logic and template files the layout or rendered content.

Author: Ben Marshall

Red Bull Addict, Self-Proclaimed Grill Master, Entrepreneur, Workaholic, Front End Engineer, SEO/SM Strategist, Web Developer, Blogger

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.