Drupal Field Formatters

I develop with Drupal 6 at work. One of the things I've been finding useful recently is writing field formatters. Field formatters are handy because they're used to format CCK field content when it gets displayed in nodes and also to format fields in Views. You can choose between formatters for each field of a node type under the 'Display' tab in the node type's settings. In Views, if you choose a display type that uses fields, you can select a different field formatter when you add each field.

To write your own field formatters, you implement hook_field_formatter_info(), then create theme functions for your formatters and add them to your hook_theme() implementation. At one point this was documented in examples packaged with CCK, but it seems the examples are no longer part of the CCK download.

A simple hook_field_formatter_info() implementation looks like this:

  1. /**
  2. * Implementation of hook_field_formatter_info().
  3. */
  4. function example_field_formatter_info() {
  5.   return array(
  6.     'myformatter' => array(
  7.       'label' => t('My Formatter'),
  8.       'field types' => array('text'),
  9.       'multiple values' => CONTENT_HANDLE_CORE,
  10.     ),
  11.   );
  12. }
  • Your formatter should have a unique handle, in this case "myformatter". Using "x_formatter" or "x_module_name" here makes the theme key name confusing.
  • The 'field types' array contains the names of cck field types applicable to this formatter.
  • 'multiple values' determines the way field content is passed to the theme function. It may be either of two values:
    • CONTENT_HANDLE_CORE means that the formatter is called separately for each value in the field. If a field contains multiple values, then the formatter is called multiple times. Field content is passed to the formatter in $element['#item']. This is the default.
    • CONTENT_HANDLE_MODULE means that the formatter is called only once for a field, and the theme formatter gets all the field values at once. In this case, field content is passed to the formatter in $element[0]['#item'], $element[1]['#item'], $element[2]['#item'], etc. Note that the $elements array contains other information about the field, such as $element['#field_name']

You will need to add the field formatter theme function to your hook_theme() implementation:

  1. /**
  2. * Implementation of hook_theme().
  3. */
  4. function exmaple_theme($existing, $type, $theme, $path) {
  5.   return array(
  6.     'example_formatter_myformatter' => array(
  7.       'function' => 'theme_myformatter',
  8.       'arguments' => array('element' => NULL),
  9.     ),
  10.   );
  11. }
  • The theme array key, in this case 'example_formatter_myformatter' is constructed from {module name}_formatter_{formatter name}.
  • You don't need to include the 'function' => 'theme_myformatter' line; the default theme function name in this case would be 'theme_example_formatter_myformatter'.

The formatter function itself should return a stuff that is ready to output to the browser:

  1. /**
  2. * Theme function for myformatter from hook_field_formatter_info().
  3. * @param $element
  4. *   Array of formatter info and the item to theme. Field contents will either be
  5. *   in $element['#item'] or $element[$i]['#item'].
  6. */
  7. function theme_example_formatter_myformatter($element) {
  8.   return strtoupper($element['#item']['safe']);
  9. }

This field formatter simply displays the contents of the text field in all caps. You'll need to clear your cache before your new formatter shows up in the field display choices.