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