function EntityViewBuilder::viewFieldItem

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

Overrides EntityViewBuilderInterface::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.