public function PictureBackgroundFormatterMedia::viewElements in Picture Background Formatter 8
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 PictureBackgroundFormatter::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ PictureBackgroundFormatterMedia.php, line 36  
Class
- PictureBackgroundFormatterMedia
 - Plugin implementation of the 'picture_background_formatter_media' formatter.
 
Namespace
Drupal\picture_background_formatter\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
  $media = $this
    ->getEntitiesToView($items, $langcode);
  $files = [];
  // Early opt-out if the field is empty.
  if (empty($media)) {
    return [];
  }
  // The parent entity, used for token replacement in the selector.
  $entity = $items
    ->getEntity();
  /** @var \Drupal\media\MediaInterface $media_item */
  foreach ($media as $delta => $media_item) {
    $type_configuration = method_exists($media_item, 'getSource') ? $media_item
      ->getSource()
      ->getConfiguration() : $media_item
      ->getType()
      ->getConfiguration();
    // Get the actual image entities from the media item.
    $image_items = isset($type_configuration['source_field']) ? $media_item
      ->get($type_configuration['source_field']) : NULL;
    foreach ($image_items as $image_item) {
      // Get each file entity.
      $files[] = $image_item->entity;
    }
  }
  return $this
    ->build_element($files, $entity);
}