‪TYPO3CMS  11.5
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 
69  public function ‪render(‪FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
70  {
71  // TODO deprecate $usedPathsRelativeToCurrentScript here as well
72  // If autoplay isn't set manually check if $file is a FileReference take autoplay from there
73  if (!isset($options['autoplay']) && $file instanceof ‪FileReference) {
74  $autoplay = $file->‪getProperty('autoplay');
75  if ($autoplay !== null) {
76  $options['autoplay'] = $autoplay;
77  }
78  }
79 
80  $attributes = [];
81  if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
82  $attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
83  }
84  if (isset($options['data']) && is_array($options['data'])) {
85  array_walk($options['data'], static function (&$value, $key) {
86  $value = 'data-' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
87  });
88  $attributes[] = implode(' ', $options['data']);
89  }
90  if ((int)$width > 0) {
91  $attributes[] = 'width="' . (int)$width . '"';
92  }
93  if ((int)$height > 0) {
94  $attributes[] = 'height="' . (int)$height . '"';
95  }
96  if (!isset($options['controls']) || !empty($options['controls'])) {
97  $attributes[] = 'controls';
98  }
99  if (!empty($options['autoplay'])) {
100  $attributes[] = 'autoplay';
101  // If autoplay is enabled, enforce muted, see https://developer.chrome.com/blog/autoplay/
102  $attributes[] = 'muted';
103  }
104  if (!empty($options['muted'])) {
105  $attributes[] = 'muted';
106  }
107  if (!empty($options['loop'])) {
108  $attributes[] = 'loop';
109  }
110  if (isset($options['additionalConfig']) && is_array($options['additionalConfig'])) {
111  foreach ($options['additionalConfig'] as $key => $value) {
112  if ((bool)$value) {
113  $attributes[] = htmlspecialchars($key);
114  }
115  }
116  }
117 
118  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'controlsList', 'preload'] as $key) {
119  if (!empty($options[$key])) {
120  $attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
121  }
122  }
123 
124  // Clean up duplicate attributes
125  $attributes = array_unique($attributes);
126 
127  return sprintf(
128  '<video%s><source src="%s" type="%s"></video>',
129  empty($attributes) ? '' : ' ' . implode(' ', $attributes),
130  htmlspecialchars((string)$file->‪getPublicUrl()),
131  $file->‪getMimeType()
132  );
133  }
134 }
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer\canRender
‪bool canRender(FileInterface $file)
Definition: VideoTagRenderer.php:53
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer
Definition: VideoTagRenderer.php:26
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer\getPriority
‪int getPriority()
Definition: VideoTagRenderer.php:42
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[], $usedPathsRelativeToCurrentScript=false)
Definition: VideoTagRenderer.php:68
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪TYPO3\CMS\Core\Resource\Rendering
Definition: AudioTagRenderer.php:16
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface
Definition: FileRendererInterface.php:25
‪TYPO3\CMS\Core\Resource\FileInterface\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
‪TYPO3\CMS\Core\Resource\FileInterface\getMimeType
‪string getMimeType()
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer\$possibleMimeTypes
‪array $possibleMimeTypes
Definition: VideoTagRenderer.php:31