You are here

public function FieldCollectionItemsFormatter::viewElements in Field collection 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/FieldCollectionItemsFormatter.php \Drupal\field_collection\Plugin\Field\FieldFormatter\FieldCollectionItemsFormatter::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 FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/FieldCollectionItemsFormatter.php, line 24

Class

FieldCollectionItemsFormatter
Plugin implementation of the 'field_collection_items' formatter.

Namespace

Drupal\field_collection\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $render_items = [];
  foreach ($items as $delta => $item) {
    if ($item->value !== NULL) {
      $render_item = \Drupal::entityTypeManager()
        ->getViewBuilder('field_collection_item')
        ->view($item
        ->getFieldCollectionItem());
      $builder = $render_item['#pre_render'][0][0];
      unset($render_item['#pre_render']);
      $render_items[] = $builder
        ->build($render_item);
    }
  }
  return $render_items;
}