function EntityViewBuilder::viewField

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::viewField()
  2. 10 core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::viewField()
  3. 8.9.x core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::viewField()

File

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

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

public function viewField(FieldItemListInterface $items, $display_options = []) {
  /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
  $entity = $items->getEntity();
  // If the field is not translatable and the entity is, then the field item
  // list always points to the default translation of the entity. Attempt to
  // fetch it in the current content language.
  if (!$items->getFieldDefinition()
    ->isTranslatable() && $entity->isTranslatable()) {
    $entity = $this->entityRepository
      ->getTranslationFromContext($entity);
  }
  $field_name = $items->getFieldDefinition()
    ->getName();
  $display = $this->getSingleFieldDisplay($entity, $field_name, $display_options);
  $output = [];
  $build = $display->build($entity);
  if (isset($build[$field_name])) {
    $output = $build[$field_name];
  }
  return $output;
}

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