function field_view_field

7.x field.module field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL)

Returns a renderable array for the value of a single field in an entity.

The resulting output is a fully themed field with label and multiple values.

This function can be used by third-party modules that need to output an isolated field.

  • Do not use inside node (or any other entity) templates; use render($content[FIELD_NAME]) instead.
  • Do not use to display all fields in an entity; use field_attach_prepare_view() and field_attach_view() instead.
  • The field_view_value() function can be used to output a single formatted field value, without label or wrapping field markup.

The function takes care of invoking the prepare_view steps. It also respects field access permissions.

Parameters

$entity_type: The type of $entity; e.g., 'node' or 'user'.

$entity: The entity containing the field to display. Must at least contain the id key and the field data to display.

$field_name: The name of the field to display.

$display: Can be either:

  • The name of a view mode. The field will be displayed according to the display settings specified for this view mode in the $instance definition for the field in the entity's bundle. If no display settings are found for the view mode, the settings for the 'default' view mode will be used.
  • An array of display settings, as found in the 'display' entry of $instance definitions. The following key/value pairs are allowed:

    • label: (string) Position of the label. The default 'field' theme implementation supports the values 'inline', 'above' and 'hidden'. Defaults to 'above'.
    • type: (string) The formatter to use. Defaults to the 'default_formatter' for the field type, specified in hook_field_info(). The default formatter will also be used if the requested formatter is not available.
    • settings: (array) Settings specific to the formatter. Defaults to the formatter's default settings, specified in hook_field_formatter_info().
    • weight: (float) The weight to assign to the renderable element. Defaults to 0.

$langcode: (Optional) The language the field values are to be shown in. The site's current language fallback logic will be applied no values are available for the language. If no language is provided the current language will be used.

Return value

A renderable array for the field value.

See also

field_view_value()

Related topics

3 calls to field_view_field()
FieldDisplayAPITestCase::testFieldViewField in drupal/modules/field/tests/field.test
Test the field_view_field() function.
field_view_value in drupal/modules/field/field.module
Returns a renderable array for a single field value.
hook_tokens_alter in drupal/modules/system/system.api.php
Alter replacement values for placeholder tokens.

File

drupal/modules/field/field.module, line 874
Attach custom data fields to Drupal entities.

Code

function field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) {
  $output = array();

  if ($field = field_info_field($field_name)) {
    if (is_array($display)) {
      // When using custom display settings, fill in default values.
      $cache = _field_info_field_cache();
      $display = $cache->prepareInstanceDisplay($display, $field["type"]);
    }

    // Hook invocations are done through the _field_invoke() functions in
    // 'single field' mode, to reuse the language fallback logic.
    // Determine the actual language to display for the field, given the
    // languages available in the field data.
    $display_language = field_language($entity_type, $entity, $field_name, $langcode);
    $options = array(
      'field_name' => $field_name,
      'language' => $display_language,
    );
    $null = NULL;

    // Invoke prepare_view steps if needed.
    if (empty($entity->_field_view_prepared)) {
      list($id) = entity_extract_ids($entity_type, $entity);

      // First let the field types do their preparation.
      _field_invoke_multiple('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
      // Then let the formatters do their own specific massaging.
      _field_invoke_multiple_default('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
    }

    // Build the renderable array.
    $result = _field_invoke_default('view', $entity_type, $entity, $display, $null, $options);

    // Invoke hook_field_attach_view_alter() to let other modules alter the
    // renderable array, as in a full field_attach_view() execution.
    $context = array(
      'entity_type' => $entity_type,
      'entity' => $entity,
      'view_mode' => '_custom',
      'display' => $display,
      'language' => $langcode,
    );
    drupal_alter('field_attach_view', $result, $context);

    if (isset($result[$field_name])) {
      $output = $result[$field_name];
    }
  }

  return $output;
}