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