function field_view_value

Returns a renderable array for a single field value.

Parameters

\Drupal\Core\Entity\EntityInterface $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.

$item: The field value to display, as found in $entity->field_name[$langcode][$delta].

$display: Can be either the name of a view mode, or an array of display settings. See field_view_field() for more information.

$langcode: (Optional) The language of the value in $item. If not provided, the current language will be assumed.

Return value

A renderable array for the field value.

Related topics

1 call to field_view_value()
DisplayApiTest::testFieldViewValue in drupal/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php
Test the field_view_value() function.

File

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

Code

function field_view_value(EntityInterface $entity, $field_name, $item, $display = array(), $langcode = NULL) {
  $output = array();
  if ($field = field_info_field($field_name)) {

    // Determine the langcode that will be used by language fallback.
    $langcode = field_language($entity, $field_name, $langcode);

    // Push the item as the single value for the field, and defer to
    // field_view_field() to build the render array for the whole field.
    $clone = clone $entity;
    $clone->{$field_name}[$langcode] = array(
      $item,
    );
    $elements = field_view_field($clone, $field_name, $display, $langcode);

    // Extract the part of the render array we need.
    $output = isset($elements[0]) ? $elements[0] : array();
    if (isset($elements['#access'])) {
      $output['#access'] = $elements['#access'];
    }
  }
  return $output;
}