Version 1
[yaffs-website] / web / modules / contrib / video / src / Plugin / Field / FieldFormatter / VideoEmbedThumbnailFormatter.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\video\Plugin\Field\FieldFormatter\VideoEmbedThumbnailFormatter.
6  */
7
8 namespace Drupal\video\Plugin\Field\FieldFormatter;
9
10 use Drupal\Core\Field\FormatterBase;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14 use Drupal\Core\Url;
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;
20
21 /**
22  * Plugin implementation of the thumbnail field formatter.
23  *
24  * @FieldFormatter(
25  *   id = "video_embed_thumbnail",
26  *   label = @Translation("Embedded Video Thumbnail"),
27  *   field_types = {
28  *     "video"
29  *   }
30  * )
31  */
32 class VideoEmbedThumbnailFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
33
34   /**
35    * The embed provider plugin manager.
36    *
37    * @var \Drupal\video_embed_field\ProviderManagerInterface
38    */
39   protected $providerManager;
40
41   /**
42    * Class constant for linking to content.
43    */
44   const LINK_CONTENT = 'content';
45
46   /**
47    * Class constant for linking to the provider URL.
48    */
49   const LINK_PROVIDER = 'provider';
50
51   /**
52    * {@inheritdoc}
53    */
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();
60     $element = [];
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);
66       $url = FALSE;
67       if ($this->getSetting('link_image_to') == static::LINK_CONTENT) {
68         $url = $items->getEntity()->urlInfo();
69       }
70       elseif ($this->getSetting('link_image_to') == static::LINK_PROVIDER) {
71         $url = Url::fromUri(file_create_url($file->getFileUri()));
72       }
73       $element[$delta] = $provider->renderThumbnail($this->getSetting('image_style'), $url);
74     }
75     return $element;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public static function defaultSettings() {
82     return [
83       'image_style' => '',
84       'link_image_to' => ''
85     ];
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function settingsForm(array $form, FormStateInterface $form_state) {
92     $form['image_style'] = [
93       '#title' => $this->t('Image Style'),
94       '#type' => 'select',
95       '#default_value' => $this->getSetting('image_style'),
96       '#required' => TRUE,
97       '#options' => image_style_options(),
98     ];
99     $form['link_image_to'] = [
100       '#title' => $this->t('Link image to'),
101       '#type' => 'select',
102       '#empty_option' => $this->t('- None -'),
103       '#default_value' => $this->getSetting('link_image_to'),
104       '#options' => [
105         static::LINK_CONTENT => $this->t('Content'),
106         static::LINK_PROVIDER => $this->t('Provider URL'),
107       ],
108     ];
109     return $form;
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function settingsSummary() {
116     $summary[] = $this->t('Video thumbnail (@quality).', ['@quality' => $this->getSetting('image_style')]);
117     return $summary;
118   }
119
120   /**
121    * Constructs a new instance of the plugin.
122    *
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
134    *   The 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.
139    */
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;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
149     return new static(
150       $plugin_id,
151       $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')
158     );
159   }
160
161   /**
162    * {@inheritdoc}
163    */
164   public static function isApplicable(FieldDefinitionInterface $field_definition) {
165     if(empty($field_definition->getTargetBundle())){
166       return TRUE;
167     }
168     else{
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'){
173         return TRUE;
174       }
175     }
176     return FALSE;
177   }
178 }