20c1369924741073fbfae11005da336db7881cfe
[yaffs-website] / web / core / modules / file / src / Plugin / Field / FieldFormatter / FileVideoFormatter.php
1 <?php
2
3 namespace Drupal\file\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Plugin implementation of the 'file_video' formatter.
9  *
10  * @FieldFormatter(
11  *   id = "file_video",
12  *   label = @Translation("Video"),
13  *   description = @Translation("Display the file using an HTML5 video tag."),
14  *   field_types = {
15  *     "file"
16  *   }
17  * )
18  */
19 class FileVideoFormatter extends FileMediaFormatterBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public static function getMediaType() {
25     return 'video';
26   }
27
28   /**
29    * {@inheritdoc}
30    */
31   public static function defaultSettings() {
32     return [
33       'muted' => FALSE,
34       'width' => 640,
35       'height' => 480,
36     ] + parent::defaultSettings();
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function settingsForm(array $form, FormStateInterface $form_state) {
43     return parent::settingsForm($form, $form_state) + [
44       'muted' => [
45         '#title' => $this->t('Muted'),
46         '#type' => 'checkbox',
47         '#default_value' => $this->getSetting('muted'),
48       ],
49       'width' => [
50         '#type' => 'number',
51         '#title' => $this->t('Width'),
52         '#default_value' => $this->getSetting('width'),
53         '#size' => 5,
54         '#maxlength' => 5,
55         '#field_suffix' => $this->t('pixels'),
56         '#min' => 0,
57         '#required' => TRUE,
58       ],
59       'height' => [
60         '#type' => 'number',
61         '#title' => $this->t('Height'),
62         '#default_value' => $this->getSetting('height'),
63         '#size' => 5,
64         '#maxlength' => 5,
65         '#field_suffix' => $this->t('pixels'),
66         '#min' => 0,
67         '#required' => TRUE,
68       ],
69     ];
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function settingsSummary() {
76     $summary = parent::settingsSummary();
77     $summary[] = $this->t('Muted: %muted', ['%muted' => $this->getSetting('muted') ? $this->t('yes') : $this->t('no')]);
78     $summary[] = $this->t('Size: %width x %height pixels', [
79       '%width' => $this->getSetting('width'),
80       '%height' => $this->getSetting('height'),
81     ]);
82     return $summary;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   protected function prepareAttributes(array $additional_attributes = []) {
89     return parent::prepareAttributes(['muted'])
90       ->setAttribute('width', $this->getSetting('width'))
91       ->setAttribute('height', $this->getSetting('height'));
92   }
93
94 }