Screencasts

Video tutorials

Theming individual CCK fields with templates and preprocess functions

If you use Drupal 6, you most likely also use CCK (mostly in core in Drupal). In this tutorial, we explore several methods you can use to theme invidual CCK fields for your content types. We'll take a look at field templates, preprocess functions and custom template suggestions.

Watch the Screencast

Tutorial In a Nutshell

To follow this tutorial, you need to install Content Construction Kit and create at least two content types with several fields (preferably shared across content types). I also highly recommend installing devel module as well as its popular add-on, devel themer.

I have created two content types - video_product and book_product – that share several custom fields of various type, including text and image.

video_product content type

Note that the book_product has different labels.

book_product content type

Then, using devel module, I have generated dummy content with these node types. Now, I need to add some custom text to the fields via the theming layer. The easiest option to accomplish this would be by copying and modifying the default template.

Using CCK field templates

CCK comes with a default field template, called content-field.tpl.php. Copy it from the module directory (cck/theme) to your theme (or subtheme if you’re using Zen or Fusion).

In addition to the default template, you also have several template suggestions based on it.

As typical with Drupal, the more specific options (e.g. the last one in the list) would have a precedence over the more generic one.

When you open content-field.tpl.php, you’ll see available variables such as $field_name or even the $node object. Scroll down and add some print statement beneath the label:

Using CCK field templates

Then go back to the site and empty the cache (site/configuration/performance). Our new message is printed on all fields now.

Using CCK field templates

But now we want this text to appear only on the description field and so we’re going to create specific templates for this field by content type. In order to do it, we need to find out the name of the field. You can do it either on the manage fields configuration page or – much easier – by using devel_themer. Simply point and click on any field and you’ll see the popup with possible templates that can be used for this field.

Using CCK field templates

Now we can go back to the IDE and copy the modified content-field.tpl.php. Rename it to content-field-field_description-product_video.tpl.php (or whatever it is on your installation). Then do the same for the book_type (content-field-field_description-product_book.tpl.php). Now I can delete the “hello world” line and change it to something different for both of the new templates.

Using CCK field templates

Using CCK field templates

Go back to the website and refresh cache. You should be able to see the changes.

Using CCK field templates

Using CCK field templates

Using Drupal preprocess functions.

In what situation preprocess functions are preferable to working directly with the templates? Well, for one thing, you’re likely to end up with a ton of different templates with a serious downside in terms of maintenance. Or, just like in our example, you might need to use some piece of code that is shared across several fields but has to display different contents depending on the field or other context.

Let’s begin by opening template.php file for your theme (create it if there isn’t one yet). Then you can add preprocess function for the field template:

function your_theme_preprocess_content_field(&$vars) {

}

To see all variable available to your template, you can use dpm($vars).

Drupal preprocess functions

When you go back to the site and refresh the page, you’ll see the nicely printed arrays of the fields (there’re going to be a bunch of them because we’re on the teaser page). Open one to check the contents. You’ll see template_files, field_name and other useful data.

Drupal preprocess functions

Let’s add our custom message as a new key-value pair to the variables array in the preprocess function:

$vars[‘message’]  = t(‘Hello world’);

Drupal preprocess functions

After you’ve saved the page and refreshed, opening the array will reveal the newly created variable (typically at the very bottom).

Drupal preprocess functions

All that’s left is actually print it in the field template file. Open it and add the following line to wherever you want:

<?php print $message; ?>

After emptying the cache, the message is printed within the fields.

Theming individual CCK fields

The next step is recreating our work with templates but this time using the preprocess function. First of all, let’s delete field specific templates to prevent them from being used by Drupal. Now we can add add or switch statements that will check the field_name:

Drupal preprocess functions

The message should only show up on the specified fields.

Creating custom template suggestions

This topic is addressed more fully in Drupal documentation. You’re not very likely to use this feature for the fields, however it’s still worth taking a look into.

For example, if I wanted to have a specially named template for the fields in teasers only, I would use this code:

Creating custom template suggestions

When you check the template_files array, it’s going to be there.

Creating custom template suggestions