function EntityViewBuilder::viewField

Same name in other branches
  1. 9 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. 11.x core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::viewField()

Overrides EntityViewBuilderInterface::viewField

1 call to EntityViewBuilder::viewField()
EntityViewBuilder::viewFieldItem in core/lib/Drupal/Core/Entity/EntityViewBuilder.php
Builds a renderable array for a single field item.

File

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

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.