You are here

public function EntityReferenceMultipleFormatter::viewElements in Entity reference multiple display 8

Same name and namespace in other branches
  1. 8.x src/Plugin/Field/FieldFormatter/EntityReferenceMultipleFormatter.php \Drupal\entityreference_multiple\Plugin\Field\FieldFormatter\EntityReferenceMultipleFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides EntityReferenceEntityFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/EntityReferenceMultipleFormatter.php, line 42

Class

EntityReferenceMultipleFormatter
Plugin implementation of the 'entityreference_entity_multiple_view' formatter.

Namespace

Drupal\entityreference_multiple\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  $view_modes = explode(',', $this
    ->getSetting('view_mode'));
  $view_mode = 'default';
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {
    if (!empty($view_modes)) {
      $view_mode = trim(array_shift($view_modes));
    }
    $view_builder = $this->entityTypeManager
      ->getViewBuilder($entity
      ->getEntityTypeId());
    $elements[$delta] = $view_builder
      ->view($entity, $view_mode, $entity
      ->language()
      ->getId());
  }
  return $elements;
}