function EntityViewBuilder::viewFieldItem

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::viewFieldItem()

File

core/lib/Drupal/Core/Entity/EntityViewBuilder.php, line 472

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

public function viewFieldItem(FieldItemInterface $item, $display = []) {
  $entity = $item->getEntity();
  $field_name = $item->getFieldDefinition()
    ->getName();
  // Clone the entity since we are going to modify field values.
  $clone = clone $entity;
  // Push the item as the single value for the field, and defer to viewField()
  // to build the render array for the whole list.
  $clone->{$field_name}
    ->setValue([
    $item->getValue(),
  ]);
  $elements = $this->viewField($clone->{$field_name}, $display);
  // Extract the part of the render array we need.
  $output = $elements[0] ?? [];
  if (isset($elements['#access'])) {
    $output['#access'] = $elements['#access'];
  }
  return $output;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.