function FileVideoFormatter::viewElements

Same name and namespace in other branches
  1. main core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\FileVideoFormatter::viewElements()

Overrides FileMediaFormatterBase::viewElements

File

core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php, line 203

Class

FileVideoFormatter
Plugin implementation of the 'file_video' formatter.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) : array {
  $elements = parent::viewElements($items, $langcode);
  /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
  $entity = $items->getEntity();
  if (!empty($this->getSetting('poster')) && !$entity->get($this->getSetting('poster'))
    ->isEmpty()) {
    $poster_file_item = $entity->get($this->getSetting('poster'))[0];
    $poster_file_entity = $poster_file_item->entity;
    // Set the entity in the correct language for display.
    if ($poster_file_entity instanceof TranslatableInterface && $poster_file_entity->hasTranslation($langcode)) {
      $poster_file_entity = $poster_file_entity->getTranslation($langcode);
    }
    $poster_image_style_id = $this->getSetting('poster_image_style');
    if (!empty($poster_image_style_id)) {
      // With imagecache selected:
      $poster_image_style = $this->imageStyleStorage
        ->load($poster_image_style_id);
      CacheableMetadata::createFromObject($poster_image_style)->applyTo($elements);
      $poster_url = $poster_image_style->buildUrl($poster_file_entity->getFileUri());
    }
    else {
      // Without imagecache:
      $poster_url = $poster_file_entity->createFileUrl();
    }
    CacheableMetadata::createFromObject($poster_file_entity)->applyTo($elements);
    $poster_attributes = new Attribute();
    $poster_attributes->setAttribute('poster', $poster_url);
    $elements[0]['#attributes']->merge($poster_attributes);
  }
  return $elements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.