152b524fb9e9ba29b5e63cf9fbd0c5d7b41fe492
[yaffs-website] / web / modules / contrib / video_embed_field / src / Plugin / Field / FieldFormatter / Video.php
1 <?php
2
3 namespace Drupal\video_embed_field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FormatterBase;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\field\Entity\FieldConfig;
12 use Drupal\video_embed_field\ProviderManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Plugin implementation of the video field formatter.
17  *
18  * @FieldFormatter(
19  *   id = "video_embed_field_video",
20  *   label = @Translation("Video"),
21  *   field_types = {
22  *     "video_embed_field"
23  *   }
24  * )
25  */
26 class Video extends FormatterBase implements ContainerFactoryPluginInterface {
27
28   /**
29    * The embed provider plugin manager.
30    *
31    * @var \Drupal\video_embed_field\ProviderManagerInterface
32    */
33   protected $providerManager;
34
35   /**
36    * The logged in user.
37    *
38    * @var \Drupal\Core\Session\AccountInterface
39    */
40   protected $currentUser;
41
42   /**
43    * Constructs a new instance of the plugin.
44    *
45    * @param string $plugin_id
46    *   The plugin_id for the formatter.
47    * @param mixed $plugin_definition
48    *   The plugin implementation definition.
49    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
50    *   The definition of the field to which the formatter is associated.
51    * @param array $settings
52    *   The formatter settings.
53    * @param string $label
54    *   The formatter label display setting.
55    * @param string $view_mode
56    *   The view mode.
57    * @param array $third_party_settings
58    *   Third party settings.
59    * @param \Drupal\video_embed_field\ProviderManagerInterface $provider_manager
60    *   The video embed provider manager.
61    * @param \Drupal\Core\Session\AccountInterface $current_user
62    *   The logged in user.
63    */
64   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, $settings, $label, $view_mode, $third_party_settings, ProviderManagerInterface $provider_manager, AccountInterface $current_user) {
65     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
66     $this->providerManager = $provider_manager;
67     $this->currentUser = $current_user;
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
74     return new static(
75       $plugin_id,
76       $plugin_definition,
77       $configuration['field_definition'],
78       $configuration['settings'],
79       $configuration['label'],
80       $configuration['view_mode'],
81       $configuration['third_party_settings'],
82       $container->get('video_embed_field.provider_manager'),
83       $container->get('current_user')
84     );
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function viewElements(FieldItemListInterface $items, $langcode) {
91     $element = [];
92     foreach ($items as $delta => $item) {
93       $provider = $this->providerManager->loadProviderFromInput($item->value);
94
95       if (!$provider) {
96         $element[$delta] = ['#theme' => 'video_embed_field_missing_provider'];
97       }
98       else {
99         $autoplay = $this->currentUser->hasPermission('never autoplay videos') ? FALSE : $this->getSetting('autoplay');
100         $element[$delta] = $provider->renderEmbedCode($this->getSetting('width'), $this->getSetting('height'), $autoplay);
101         $element[$delta]['#cache']['contexts'][] = 'user.permissions';
102
103         // For responsive videos, wrap each field item in it's own container.
104         if ($this->getSetting('responsive')) {
105           $element[$delta] = [
106             '#type' => 'container',
107             '#attached' => ['library' => ['video_embed_field/responsive-video']],
108             '#attributes' => ['class' => ['video-embed-field-responsive-video']],
109             'children' => $element[$delta],
110           ];
111         }
112       }
113
114     }
115     return $element;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public static function defaultSettings() {
122     return [
123       'responsive' => TRUE,
124       'width' => '854',
125       'height' => '480',
126       'autoplay' => TRUE,
127     ];
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function settingsForm(array $form, FormStateInterface $form_state) {
134     $elements = parent::settingsForm($form, $form_state);
135     $elements['autoplay'] = [
136       '#title' => $this->t('Autoplay'),
137       '#type' => 'checkbox',
138       '#description' => $this->t('Autoplay the videos for users without the "never autoplay videos" permission. Roles with this permission will bypass this setting.'),
139       '#default_value' => $this->getSetting('autoplay'),
140     ];
141     $elements['responsive'] = [
142       '#title' => $this->t('Responsive Video'),
143       '#type' => 'checkbox',
144       '#description' => $this->t("Make the video fill the width of it's container, adjusting to the size of the user's screen."),
145       '#default_value' => $this->getSetting('responsive'),
146     ];
147     // Loosely match the name attribute so forms which don't have a field
148     // formatter structure (such as the WYSIWYG settings form) are also matched.
149     $responsive_checked_state = [
150       'visible' => [
151         [
152           ':input[name*="responsive"]' => ['checked' => FALSE],
153         ],
154       ],
155     ];
156     $elements['width'] = [
157       '#title' => $this->t('Width'),
158       '#type' => 'number',
159       '#field_suffix' => 'px',
160       '#default_value' => $this->getSetting('width'),
161       '#required' => TRUE,
162       '#size' => 20,
163       '#states' => $responsive_checked_state,
164     ];
165     $elements['height'] = [
166       '#title' => $this->t('Height'),
167       '#type' => 'number',
168       '#field_suffix' => 'px',
169       '#default_value' => $this->getSetting('height'),
170       '#required' => TRUE,
171       '#size' => 20,
172       '#states' => $responsive_checked_state,
173     ];
174     return $elements;
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public function settingsSummary() {
181     $dimensions = $this->getSetting('responsive') ? $this->t('Responsive') : $this->t('@widthx@height', ['@width' => $this->getSetting('width'), '@height' => $this->getSetting('height')]);
182     $summary[] = $this->t('Embedded Video (@dimensions@autoplay).', [
183       '@dimensions' => $dimensions,
184       '@autoplay' => $this->getSetting('autoplay') ? $this->t(', autoplaying') : '',
185     ]);
186     return $summary;
187   }
188
189   /**
190    * Get an instance of the Video field formatter plugin.
191    *
192    * This is useful because there is a lot of overlap to the configuration and
193    * display of a video in a WYSIWYG and configuring a field formatter. We
194    * get an instance of the plugin with our own WYSIWYG settings shimmed in,
195    * as well as a fake field_definition because one in this context doesn't
196    * exist. This allows us to reuse aspects such as the form and settings
197    * summary for the WYSIWYG integration.
198    *
199    * @param array $settings
200    *   The settings to pass to the plugin.
201    *
202    * @return static
203    *   The formatter plugin.
204    */
205   public static function mockInstance($settings) {
206     return \Drupal::service('plugin.manager.field.formatter')->createInstance('video_embed_field_video', [
207       'settings' => !empty($settings) ? $settings : [],
208       'third_party_settings' => [],
209       'field_definition' => new FieldConfig([
210         'field_name' => 'mock',
211         'entity_type' => 'mock',
212         'bundle' => 'mock',
213       ]),
214       'label' => '',
215       'view_mode' => '',
216     ]);
217   }
218
219 }