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