You are here

function content_format in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 5 content.module \content_format()
  2. 6.3 content.module \content_format()
  3. 6.2 content.module \content_format()

Format a field item for display.

Used to display a field's values outside the context of the $node, as when fields are displayed in Views, or to display a field in a template using a different formatter than the one set up on the Display Fields tab for the node's context.

Parameters

$field: Either a field array or the name of the field.

$item: The field item(s) to be formatted (such as $node->field_foo[0], or $node->field_foo if the formatter handles multiple values itself)

$formatter: The name of the formatter to use.

$node: Optionally, the containing node object for context purposes and field-instance options.

Return value

A string containing the contents of the field item(s) sanitized for display. It will have been passed through the necessary check_plain() or check_markup() functions as necessary.

2 calls to content_format()
views_handler_field_content::render in includes/content.views.inc
views_handler_field_content_multiple::render in includes/content.views.inc

File

./content.module, line 1515
Allows administrators to associate custom fields to content types.

Code

function content_format($field, $item, $formatter = 'default', $node = NULL) {
  if (!is_array($field)) {
    $field = content_fields($field);
  }
  $field_types = _content_field_types();
  $formatters = $field_types[$field['type']]['formatters'];
  if (!isset($formatters[$formatter])) {
    $formatter = 'default';
  }
  $formatter_name = $formatter;
  $formatter = $formatters[$formatter_name];
  $theme = $formatter['module'] . '_formatter_' . $formatter_name;
  $element = array(
    '#theme' => $theme,
    '#field_name' => $field['field_name'],
    '#type_name' => isset($node->type) ? $node->type : '',
    '#formatter' => $formatter_name,
  );
  if (content_handle('formatter', 'multiple values', $formatter) == CONTENT_HANDLE_CORE) {

    // Single value formatter.
    // hook_field('sanitize') expects an array of items, so we build one.
    $items = array(
      $item,
    );
    $function = $field['module'] . '_field';
    if (function_exists($function)) {
      $function('sanitize', $node, $field, $items, FALSE, FALSE);
    }
    $element['#item'] = $items[0];
  }
  else {

    // Multiple values formatter.
    $items = $item;
    $function = $field['module'] . '_field';
    if (function_exists($function)) {
      $function('sanitize', $node, $field, $items, FALSE, FALSE);
    }
    foreach ($items as $delta => $item) {
      $element[$delta] = array(
        '#item' => $item,
        '#weight' => $delta,
      );
    }
  }
  return theme($theme, $element);
}