Creating a Preprocess Function

Any “complicated” functionality that exists in your tpl.php files can (and probably should) be moved to the master controller file, template.php. This will allow you to work with simple variables instead of having to worry about whether shifting that part of the page will break the other part of something else. Once you've got the hang of them, creating preprocess functions is really easy.

Let's start with an easy scenario.

You don't like the way the "submitted by" variable gets printed on nodes. In the template file node.tpl.php you replace the variable which prints the “submitted by” information using the following snippet:
<?php print date_format(date_create($created), "M j"); ?>

The PHP functions in this snippet are:

Although you can leave this functionality in the node.tpl.php file, it would be much cleaner to only print out a variable that contains the correctly formatted date. To be able to print a new variable in your tpl.php file you first need to create that variable. This is done in the file template.php with preprocess functions.

Instructions

To create your new variable you must complete the following steps:

  1. Open the file template.php and create a new preprocess function for nodes (assuming it doesn't already exist). If you don't have a template.php file yet, create a new file and add <?php to the first line.
    function MYTHEMENAME_preprocess_node (&$vars) {
    // my new variables will be added here
    }
  2. Pick a new variable name for your customized date information. For example: $created_fix.
  3. Create a new variable inside the function which will contain all of your "fancy" code calculates. Name the variable with the preprocess-appropriate name $vars['created_fix'].
  4. Copy all of the “fancy” formatting code from the file node.tpl.php.
  5. Paste the copied code from the previous step into the preprocess function so that your code is being assigned to a new template variable. For example (all on one line):
    $vars['created_fix'] = date_format(date_create($vars['created'), "M j");
  6. Save the updated file template.php.
  7. In the file node.tpl.php add the following snippet where you want the date to be printed:
    <?php print $created_fix; ?>
  8. Save the updated file node.tpl.php.
  9. Upload your two updated files to your Web server.
  10. Clear the theme registry.

The contents of your new theme variable should now appear.

Code Summary

In your theme's template.php file you should now have:
function MYTHEMENAME_preprocess_node (&$vars) {
$vars['created_fix'] = date_format(date_create($vars['created'), "M j");
}

And in your node.tpl.php file you should now have:
<?php print $created_fix; ?>

PHP for Drupal Workbook

This tutorial has also been expanded into a 30 page workbook PHP for Drupal Designers.

Want More Tutorials?

Are you interested in getting even more tutorials sent to your inbox? Sign up for our incredibly popular tips mailing list and get a free tutorial every month. You may even win a coupon for a free e-book (include your whole name if you want to win stuff).

For a limited time we'll even send you a bonus tips sheet The Top Ten Mistakes People Make when Theming Drupal when you sign up for the list.