field_format

  1. drupal
    1. drupal7
Versionen
drupal7 field_format($obj_type, $object, $field, $item, $formatter_name = NULL, $formatter_settings = array())

Format a field item for display.

TODO D7 : do we still need field_format ?

  • backwards compatibility of templates - check what fallbacks we can propose...
  • used by Views integration in CCK D6

At least needs a little rehaul/update...

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.

Übergabeparameter

$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.

Rückgabewert

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.

Verwandte Themen

Code

modules/field/field.module, line 394

<?php
function field_format($obj_type, $object, $field, $item, $formatter_name = NULL, $formatter_settings = array()) {
  if (!is_array($field)) {
    $field = field_info_field($field);
  }

  if (field_access('view', $field)) {
    // Basically, we need $field, $instance, $obj_type, $object to be able to display a value...
    list(, , $bundle) = field_attach_extract_ids($obj_type, $object);
    $instance = field_info_instance($field['field_name'], $bundle);

    $display = array(
      'type' => $formatter_name,
      'settings' => $formatter_settings,
    );
    $display = _field_get_formatter($display, $field);
    if ($display['type'] && $display['type'] !== 'hidden') {
      $theme = $formatter['module'] . '_formatter_' . $display['type'];

      $element = array(
        '#theme' => $theme,
        '#field_name' => $field['field_name'],
        '#bundle' => $bundle,
        '#formatter' => $display['type'],
        '#settings' => $display['settings'],
        '#object' => $object,
        '#delta' => isset($item['#delta']) ? $item['#delta'] : NULL,
      );

      if (field_behaviors_formatter('multiple values', $display) == FIELD_BEHAVIOR_DEFAULT) {
        // Single value formatter.

        // hook_field('sanitize') expects an array of items, so we build one.
        $items = array($item);
        $function = $field['module'] . '_field_sanitize';
        if (function_exists($function)) {
          $function($obj_type, $object, $field, $instance, $items);
        }

        $element['#item'] = $items[0];
      }
      else {
        // Multiple values formatter.
        $items = $item;
        $function = $field['module'] . '_field_sanitize';
        if (function_exists($function)) {
          $function($obj_type, $object, $field, $instance, $items);
        }

        foreach ($items as $delta => $item) {
          $element[$delta] = array(
            '#item' => $item,
            '#weight' => $delta,
          );
        }
      }

      return theme($theme, $element);
    }
  }
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen