[FEATURE] Render additionalAttributes and data-* attr in media renderer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Rendering / VideoTagRenderer.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Rendering;
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
17 use TYPO3\CMS\Core\Resource\FileInterface;
18 use TYPO3\CMS\Core\Resource\FileReference;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Class VideoTagRenderer
23 */
24 class VideoTagRenderer implements FileRendererInterface
25 {
26 /**
27 * Mime types that can be used in the HTML Video tag
28 *
29 * @var array
30 */
31 protected $possibleMimeTypes = ['video/mp4', 'video/webm', 'video/ogg', 'video/x-m4v', 'application/ogg'];
32
33 /**
34 * Returns the priority of the renderer
35 * This way it is possible to define/overrule a renderer
36 * for a specific file type/context.
37 * For example create a video renderer for a certain storage/driver type.
38 * Should be between 1 and 100, 100 is more important than 1
39 *
40 * @return int
41 */
42 public function getPriority()
43 {
44 return 1;
45 }
46
47 /**
48 * Check if given File(Reference) can be rendered
49 *
50 * @param FileInterface $file File or FileReference to render
51 * @return bool
52 */
53 public function canRender(FileInterface $file)
54 {
55 return in_array($file->getMimeType(), $this->possibleMimeTypes, true);
56 }
57
58 /**
59 * Render for given File(Reference) HTML output
60 *
61 * @param FileInterface $file
62 * @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
63 * @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
64 * @param array $options controls = TRUE/FALSE (default TRUE), autoplay = TRUE/FALSE (default FALSE), loop = TRUE/FALSE (default FALSE)
65 * @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
66 * @return string
67 */
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 (is_array($options['additionalAttributes'])) {
81 $attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
82 }
83 if (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 foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'controlsList'] as $key) {
108 if (!empty($options[$key])) {
109 $attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
110 }
111 }
112
113 return sprintf(
114 '<video%s><source src="%s" type="%s"></video>',
115 empty($attributes) ? '' : ' ' . implode(' ', $attributes),
116 htmlspecialchars($file->getPublicUrl($usedPathsRelativeToCurrentScript)),
117 $file->getMimeType()
118 );
119 }
120 }