Introduction to Drupal Preprocess & Process Functions

Drupal Preprocess Process Functions Introduction

[dropcap type=”v2″]I[/dropcap]n this post, I’m going to give you an introduction to Drupal’s powerful preprocess and process functions. If you’re not already familiar with these functions, I like to think of them as both a front-enders nightmare and best friend. A nightmare only because it’s Drupal and with everything Drupal, has a steep learning curve. A best friend because, it provides the front-ender more control over the output of rendered code allowing for more efficient, clean, and crisp markup. It does need some basic PHP knowledge, but hopefully after reading this post everything will make sense.

Before I begin, it’s a good idea to have some knowledge of Drupal’s template files. Those files are used mainly to format markup and print variables. Many times, developers will use these instead of Drupal preprocess process functions. That is often the wrong way to go. A general rule of thumb to follow is to use templates files for content and Drupal preprocess process functions to change the data before it get’s rendered into HTML.

By definition, “preprocess” is a phase of processing that happens before templates are rendered. “Process” functions, which are new in Drupal 7, serve the same purpose, with the only difference being that they run later (after preprocess) in the processing cycle.

Drupal’s Preprocess Functions

Make sense so far? Now let’s look at Drupal’s template_preprocess() function as an example. 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:

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

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

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