VideoExtendedFormatter.php in Media Thumbnails Video 8
File
src/Plugin/Field/FieldFormatter/VideoExtendedFormatter.php
View source
<?php
namespace Drupal\media_thumbnails_video\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\file\Plugin\Field\FieldFormatter\FileVideoFormatter;
use Drupal\media\MediaInterface;
class VideoExtendedFormatter extends FileVideoFormatter {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
foreach ($elements as $key => $element) {
$attributes = $this
->prepareAttributes();
$item = $items[$key];
$source_entity = $item
->getEntity();
$poster = $this
->getPoster($source_entity);
if ($poster) {
$attributes
->setAttribute('poster', $poster);
}
$elements[$key]['#theme'] = 'file_video';
$elements[$key]['#attributes'] = $attributes;
}
return $elements;
}
protected function getPoster(EntityInterface $entity) {
if ($entity instanceof MediaInterface) {
$thumbnail = $entity
->get('thumbnail')->entity;
if ($thumbnail) {
$url = file_create_url($thumbnail
->getFileUri());
return $url;
}
}
return FALSE;
}
}