function EntityViewBuilder::viewFieldItem

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

Overrides EntityViewBuilderInterface::viewFieldItem

File

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

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 = isset($elements[0]) ? $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.