5 * Contains \Drupal\video\Plugin\Field\FieldFormatter\VideoEmbedThumbnailFormatter.
8 namespace Drupal\video\Plugin\Field\FieldFormatter;
10 use Drupal\Core\Field\FormatterBase;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
15 use Drupal\video\ProviderManagerInterface;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Drupal\file\Entity\File;
18 use Drupal\Core\File\FileSystem;
19 use Drupal\Core\Field\FieldDefinitionInterface;
22 * Plugin implementation of the thumbnail field formatter.
25 * id = "video_embed_thumbnail",
26 * label = @Translation("Embedded Video Thumbnail"),
32 class VideoEmbedThumbnailFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
35 * The embed provider plugin manager.
37 * @var \Drupal\video_embed_field\ProviderManagerInterface
39 protected $providerManager;
42 * Class constant for linking to content.
44 const LINK_CONTENT = 'content';
47 * Class constant for linking to the provider URL.
49 const LINK_PROVIDER = 'provider';
54 public function viewElements(FieldItemListInterface $items, $langcode) {
55 // load widget settings
56 $field_definition = $this->fieldDefinition;
57 $entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default');
58 $widget = $entity_form_display->getRenderer($field_definition->getName());
59 $widget_settings = $widget->getSettings();
61 foreach ($items as $delta => $item) {
62 $file = File::load($item->target_id);
63 $metadata = isset($item->data) ? unserialize($item->data) : array();
64 $scheme = file_uri_scheme($file->getFileUri());
65 $provider = $this->providerManager->loadProviderFromStream($scheme, $file, $metadata, $widget_settings);
67 if ($this->getSetting('link_image_to') == static::LINK_CONTENT) {
68 $url = $items->getEntity()->urlInfo();
70 elseif ($this->getSetting('link_image_to') == static::LINK_PROVIDER) {
71 $url = Url::fromUri(file_create_url($file->getFileUri()));
73 $element[$delta] = $provider->renderThumbnail($this->getSetting('image_style'), $url);
81 public static function defaultSettings() {
91 public function settingsForm(array $form, FormStateInterface $form_state) {
92 $form['image_style'] = [
93 '#title' => $this->t('Image Style'),
95 '#default_value' => $this->getSetting('image_style'),
97 '#options' => image_style_options(),
99 $form['link_image_to'] = [
100 '#title' => $this->t('Link image to'),
102 '#empty_option' => $this->t('- None -'),
103 '#default_value' => $this->getSetting('link_image_to'),
105 static::LINK_CONTENT => $this->t('Content'),
106 static::LINK_PROVIDER => $this->t('Provider URL'),
115 public function settingsSummary() {
116 $summary[] = $this->t('Video thumbnail (@quality).', ['@quality' => $this->getSetting('image_style')]);
121 * Constructs a new instance of the plugin.
123 * @param string $plugin_id
124 * The plugin_id for the formatter.
125 * @param mixed $plugin_definition
126 * The plugin implementation definition.
127 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
128 * The definition of the field to which the formatter is associated.
129 * @param array $settings
130 * The formatter settings.
131 * @param string $label
132 * The formatter label display setting.
133 * @param string $view_mode
135 * @param array $third_party_settings
136 * Third party settings.
137 * @param \Drupal\video\ProviderManagerInterface $provider_manager
138 * The video embed provider manager.
140 public function __construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, ProviderManagerInterface $provider_manager) {
141 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
142 $this->providerManager = $provider_manager;
148 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
152 $configuration['field_definition'],
153 $configuration['settings'],
154 $configuration['label'],
155 $configuration['view_mode'],
156 $configuration['third_party_settings'],
157 $container->get('video.provider_manager')
164 public static function isApplicable(FieldDefinitionInterface $field_definition) {
165 if(empty($field_definition->getTargetBundle())){
169 $entity_form_display = entity_get_form_display($field_definition->getTargetEntityTypeId(), $field_definition->getTargetBundle(), 'default');
170 $widget = $entity_form_display->getRenderer($field_definition->getName());
171 $widget_id = $widget->getBaseId();
172 if($widget_id == 'video_embed'){