View source
<?php
namespace Drupal\video\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\video\ProviderManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\file\Entity\File;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
class VideoEmbedThumbnailFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $providerManager;
protected $streamWrapperManager;
const LINK_CONTENT = 'content';
const LINK_PROVIDER = 'provider';
public function viewElements(FieldItemListInterface $items, $langcode) {
$field_definition = $this->fieldDefinition;
$form_mode = 'default';
$entity_form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load($field_definition
->getTargetEntityTypeId() . '.' . $field_definition
->getTargetBundle() . '.' . $form_mode);
if (!$entity_form_display) {
$entity_form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->create([
'targetEntityType' => $field_definition
->getTargetEntityTypeId(),
'bundle' => $field_definition
->getTargetBundle(),
'mode' => $form_mode,
'status' => TRUE,
]);
}
$widget = $entity_form_display
->getRenderer($field_definition
->getName());
$widget_settings = $widget
->getSettings();
$element = [];
foreach ($items as $delta => $item) {
$file = File::load($item->target_id);
$metadata = isset($item->data) ? unserialize($item->data) : [];
$scheme = $this->streamWrapperManager
->getScheme($file
->getFileUri());
$provider = $this->providerManager
->loadProviderFromStream($scheme, $file, $metadata, $widget_settings);
$url = FALSE;
if ($this
->getSetting('link_image_to') == static::LINK_CONTENT) {
$url = $items
->getEntity()
->toUrl();
}
elseif ($this
->getSetting('link_image_to') == static::LINK_PROVIDER) {
$url = Url::fromUri(file_create_url($file
->getFileUri()));
}
$element[$delta] = $provider
->renderThumbnail($this
->getSetting('image_style'), $url);
}
return $element;
}
public static function defaultSettings() {
return [
'image_style' => '',
'link_image_to' => '',
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = [];
$form['image_style'] = [
'#title' => $this
->t('Image Style'),
'#type' => 'select',
'#default_value' => $this
->getSetting('image_style'),
'#required' => TRUE,
'#options' => image_style_options(),
];
$form['link_image_to'] = [
'#title' => $this
->t('Link image to'),
'#type' => 'select',
'#empty_option' => $this
->t('- None -'),
'#default_value' => $this
->getSetting('link_image_to'),
'#options' => [
static::LINK_CONTENT => $this
->t('Content'),
static::LINK_PROVIDER => $this
->t('Provider URL'),
],
];
return $form;
}
public function settingsSummary() {
$summary[] = $this
->t('Video thumbnail (@quality).', [
'@quality' => $this
->getSetting('image_style'),
]);
return $summary;
}
public function __construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, ProviderManagerInterface $provider_manager, StreamWrapperManagerInterface $stream_wrapper_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->providerManager = $provider_manager;
$this->streamWrapperManager = $stream_wrapper_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('video.provider_manager'), $container
->get('stream_wrapper_manager'));
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
if (empty($field_definition
->getTargetBundle())) {
return TRUE;
}
else {
$form_mode = 'default';
$entity_form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load($field_definition
->getTargetEntityTypeId() . '.' . $field_definition
->getTargetBundle() . '.' . $form_mode);
if (!$entity_form_display) {
$entity_form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->create([
'targetEntityType' => $field_definition
->getTargetEntityTypeId(),
'bundle' => $field_definition
->getTargetBundle(),
'mode' => $form_mode,
'status' => TRUE,
]);
}
$widget = $entity_form_display
->getRenderer($field_definition
->getName());
$widget_id = $widget
->getBaseId();
if ($widget_id == 'video_embed') {
return TRUE;
}
}
return FALSE;
}
}