You are here

function content_format in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 5 content.module \content_format()
  2. 6 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_name: 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()
content_handler_field::render in includes/views/handlers/content_handler_field.inc
content_handler_field_multiple::render in includes/views/handlers/content_handler_field_multiple.inc

File

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

Code

function content_format($field, $item, $formatter_name = 'default', $node = NULL) {
  if (!is_array($field)) {
    $field = content_fields($field);
  }
  if (content_access('view', $field, NULL, $node) && ($formatter = _content_get_formatter($formatter_name, $field['type']))) {
    $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,
      '#node' => $node,
      '#delta' => isset($item['#delta']) ? $item['#delta'] : NULL,
    );
    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);
  }
}