Security update for Core, with self-updated composer
[yaffs-website] / web / modules / contrib / videojs / src / Plugin / Field / FieldFormatter / VideoJsPlayerFormatter.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\videojs\Plugin\Field\FieldFormatter\VideJsPlayerFormatter.
6  */
7
8 namespace Drupal\videojs\Plugin\Field\FieldFormatter;
9
10 use Drupal\Core\Field\FieldItemListInterface;
11 use Drupal\Core\Field\FieldDefinitionInterface;
12 use Drupal\Core\Link;
13 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14 use Drupal\Core\Session\AccountInterface;
15 use Drupal\Core\Url;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Drupal\Core\Form\FormStateInterface;
18 use Drupal\Core\Cache\Cache;
19
20 /**
21  * Plugin implementation of the 'videojs_player' formatter.
22  *
23  * @FieldFormatter(
24  *   id = "videojs_player",
25  *   label = @Translation("Video.js Player"),
26  *   field_types = {
27  *     "file",
28  *     "video",
29  *   }
30  * )
31  */
32
33 class VideoJsPlayerFormatter extends VideoJsPlayerFormatterBase implements ContainerFactoryPluginInterface {
34
35   /**
36    * The current user.
37    *
38    * @var \Drupal\Core\Session\AccountInterface
39    */
40   protected $currentUser;
41
42   /**
43    * Constructs an VideoPlayerFormatter object.
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    *   Any third party settings settings.
59    * @param \Drupal\Core\Session\AccountInterface $current_user
60    *   The current user.
61    */
62   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user) {
63     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
64     $this->currentUser = $current_user;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
71     return new static(
72       $plugin_id,
73       $plugin_definition,
74       $configuration['field_definition'],
75       $configuration['settings'],
76       $configuration['label'],
77       $configuration['view_mode'],
78       $configuration['third_party_settings'],
79       $container->get('current_user')
80     );
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public static function defaultSettings() {
87     return array(
88       'width' => '854',
89       'height' => '480',
90       'controls' => TRUE,
91       'autoplay' => FALSE,
92       'loop' => FALSE,
93       'muted' => FALSE,
94       'preload' => 'none'
95     ) + parent::defaultSettings();
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function settingsForm(array $form, FormStateInterface $form_state) {
102     $element['width'] = [
103       '#title' => t('Width'),
104       '#type' => 'textfield',
105       '#default_value' => $this->getSetting('width'),
106       '#required' => TRUE,
107     ];
108     $element['height'] = [
109       '#title' => t('Height'),
110       '#type' => 'textfield',
111       '#default_value' => $this->getSetting('height'),
112       '#required' => TRUE,
113     ];
114     $element['controls'] = [
115       '#title' => t('Show controls'),
116       '#type' => 'checkbox',
117       '#default_value' => $this->getSetting('controls'),
118     ];
119     $element['autoplay'] = [
120       '#title' => t('Autoplay'),
121       '#type' => 'checkbox',
122       '#default_value' => $this->getSetting('autoplay'),
123     ];
124     $element['loop'] = [
125       '#title' => t('Loop'),
126       '#type' => 'checkbox',
127       '#default_value' => $this->getSetting('loop'),
128     ];
129     $element['muted'] = [
130       '#title' => t('Muted'),
131       '#type' => 'checkbox',
132       '#default_value' => $this->getSetting('muted'),
133     ];
134     $element['preload'] = [
135       '#title' => t('Preload'),
136       '#type' => 'select',
137       '#default_value' => $this->getSetting('preload'),
138       '#options' => array(
139         'none' =>'none',
140         'metadata' => 'metadata',
141         'auto' => 'auto'
142       ),
143       '#description' => t('Hint to the browser about whether optimistic downloading of the video itself or its metadata is considered worthwhile.')
144     ];
145     return $element;
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function settingsSummary() {
152     $summary = array();
153     $summary[] = t('HTML5 Video (@widthx@height@controls@autoplay@loop@muted).', [
154       '@width' => $this->getSetting('width'),
155       '@height' => $this->getSetting('height'),
156       '@controls' => $this->getSetting('controls') ? t(', controls') : '' ,
157       '@autoplay' => $this->getSetting('autoplay') ? t(', autoplaying') : '' ,
158       '@loop' => $this->getSetting('loop') ? t(', looping') : '' ,
159       '@muted' => $this->getSetting('muted') ? t(', muted') : '',
160     ]);
161     return $summary;
162   }
163   /**
164    * {@inheritdoc}
165    */
166   public function viewElements(FieldItemListInterface $items, $langcode) {
167     $elements = array();
168     $files = $this->getEntitiesToView($items, $langcode);
169
170     // Early opt-out if the field is empty.
171     if (empty($files)) {
172       return $elements;
173     }
174
175     // Collect cache tags to be added for each item in the field.
176     foreach ($files as $delta => $file) {
177       $video_uri = $file->getFileUri();
178       $elements[$delta] = array(
179         '#theme' => 'videojs',
180         '#items' => array(Url::fromUri(file_create_url($video_uri))),
181         '#player_attributes' => $this->getSettings(),
182         '#attached' => array(
183           'library' => array('videojs/videojs'),
184         ),
185       );
186     }
187     return $elements;
188   }
189
190   /**
191    * {@inheritdoc}
192    */
193   public static function isApplicable(FieldDefinitionInterface $field_definition) {
194     return !$field_definition->isList();
195   }
196
197 }