You are here

function text_field_formatter in Content Construction Kit (CCK) 6

Same name in this branch
  1. 6 examples/simple_field.php \text_field_formatter()
  2. 6 examples/example_field.php \text_field_formatter()
Same name and namespace in other branches
  1. 5 text.module \text_field_formatter()

Implementation of hook_field_formatter().

Prepare an individual item for viewing in a browser.

Parameters

$field: The field the action is being performed on.

$item: An array, keyed by column, of the data stored for this item in this field.

$formatter: The name of the formatter being used to display the field.

$node: The node object, for context. Will be NULL in some cases. Warning : when displaying field retrieved by Views, $node will not be a "full-fledged" node object, but an object containg the data returned by the Views query (at least nid, vid, changed)

Return value

An HTML string containing the formatted item.

In a multiple-value field scenario, this function will be called once per value currently stored in the field. This function is also used as the handler for viewing a field in a views.module tabular listing.

It is important that this function at the minimum perform security transformations such as running check_plain() or check_markup().

File

examples/example_field.php, line 425
These hooks are defined by field modules, modules that define a new kind of field for insertion in a content type.

Code

function text_field_formatter($field, $item, $formatter, $node) {
  if (!isset($item['value'])) {
    return '';
  }
  $allowed_values = content_allowed_values($field);
  if (!empty($allowed_values) && isset($allowed_values[$item['value']])) {
    return $allowed_values[$item['value']];
  }
  switch ($formatter) {
    case 'plain':
      $text = strip_tags($item['value']);
      break;
    case 'trimmed':
      $text = node_teaser($item['value'], $field['text_processing'] ? $item['format'] : NULL);
      break;
    case 'default':
      $text = $item['value'];
  }

  // TODO : undefined index text_processing / format on node preview
  if ($field['text_processing']) {
    return check_markup($text, $item['format'], is_null($node) || $node->build_mode == NODE_BUILD_PREVIEW);
  }
  else {
    return check_plain($text);
  }
}