‪TYPO3CMS  ‪main
VideoTagRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
26 {
32  protected ‪$possibleMimeTypes = ['video/mp4', 'video/webm', 'video/ogg', 'video/x-m4v', 'application/ogg'];
33 
43  public function ‪getPriority()
44  {
45  return 1;
46  }
47 
54  public function ‪canRender(‪FileInterface $file)
55  {
56  return in_array($file->getMimeType(), $this->possibleMimeTypes, true);
57  }
58 
67  public function ‪render(‪FileInterface $file, $width, $height, array $options = [])
68  {
69  // If autoplay isn't set manually check if $file is a FileReference take autoplay from there
70  if (!isset($options['autoplay']) && $file instanceof ‪FileReference) {
71  $autoplay = $file->‪getProperty('autoplay');
72  if ($autoplay !== null) {
73  $options['autoplay'] = $autoplay;
74  }
75  }
76 
77  $attributes = [];
78  if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
79  $attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
80  }
81  if (isset($options['data']) && is_array($options['data'])) {
82  array_walk($options['data'], static function (string &$value, string $key): void {
83  $value = 'data-' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
84  });
85  $attributes[] = implode(' ', $options['data']);
86  }
87  if ((int)$width > 0) {
88  $attributes[] = 'width="' . (int)$width . '"';
89  }
90  if ((int)$height > 0) {
91  $attributes[] = 'height="' . (int)$height . '"';
92  }
93  if (!isset($options['controls']) || !empty($options['controls'])) {
94  $attributes[] = 'controls';
95  }
96  if (!empty($options['autoplay'])) {
97  $attributes[] = 'autoplay';
98  // If autoplay is enabled, enforce muted, see https://developer.chrome.com/blog/autoplay/
99  $attributes[] = 'muted';
100  }
101  if (!empty($options['muted'])) {
102  $attributes[] = 'muted';
103  }
104  if (!empty($options['loop'])) {
105  $attributes[] = 'loop';
106  }
107  if (isset($options['additionalConfig']) && is_array($options['additionalConfig'])) {
108  foreach ($options['additionalConfig'] as $key => $value) {
109  if ($value) {
110  if ((int)$value !== 1) {
111  $attributes[] = htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
112  } else {
113  $attributes[] = htmlspecialchars($key);
114  }
115  // Ensure that the property is not set afterwards
116  $options[$key] = false;
117  }
118  }
119  }
120 
121  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'controlsList', 'preload'] as $key) {
122  if (!empty($options[$key])) {
123  $attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
124  }
125  }
126 
127  // Clean up duplicate attributes
128  $attributes = array_unique($attributes);
129 
130  return sprintf(
131  '<video%s><source src="%s" type="%s"></video>',
132  empty($attributes) ? '' : ' ' . implode(' ', $attributes),
133  htmlspecialchars((string)$file->getPublicUrl()),
134  $file->getMimeType()
135  );
136  }
137 }
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer\canRender
‪bool canRender(FileInterface $file)
Definition: VideoTagRenderer.php:53
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer
Definition: VideoTagRenderer.php:26
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[])
Definition: VideoTagRenderer.php:66
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer\getPriority
‪int getPriority()
Definition: VideoTagRenderer.php:42
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Core\Resource\Rendering
Definition: AudioTagRenderer.php:16
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface
Definition: FileRendererInterface.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪getProperty(string $key)
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer\$possibleMimeTypes
‪array $possibleMimeTypes
Definition: VideoTagRenderer.php:31