[FEATURE] Render additionalAttributes and data-* attr in media renderer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Rendering / VimeoRenderer.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\File;
18 use TYPO3\CMS\Core\Resource\FileInterface;
19 use TYPO3\CMS\Core\Resource\FileReference;
20 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
21 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Vimeo renderer class
26 */
27 class VimeoRenderer implements FileRendererInterface
28 {
29 /**
30 * @var OnlineMediaHelperInterface
31 */
32 protected $onlineMediaHelper;
33
34 /**
35 * Returns the priority of the renderer
36 * This way it is possible to define/overrule a renderer
37 * for a specific file type/context.
38 * For example create a video renderer for a certain storage/driver type.
39 * Should be between 1 and 100, 100 is more important than 1
40 *
41 * @return int
42 */
43 public function getPriority()
44 {
45 return 1;
46 }
47
48 /**
49 * Check if given File(Reference) can be rendered
50 *
51 * @param FileInterface $file File of FileReference to render
52 * @return bool
53 */
54 public function canRender(FileInterface $file)
55 {
56 return ($file->getMimeType() === 'video/vimeo' || $file->getExtension() === 'vimeo') && $this->getOnlineMediaHelper($file) !== false;
57 }
58
59 /**
60 * Get online media helper
61 *
62 * @param FileInterface $file
63 * @return bool|OnlineMediaHelperInterface
64 */
65 protected function getOnlineMediaHelper(FileInterface $file)
66 {
67 if ($this->onlineMediaHelper === null) {
68 $orgFile = $file;
69 if ($orgFile instanceof FileReference) {
70 $orgFile = $orgFile->getOriginalFile();
71 }
72 if ($orgFile instanceof File) {
73 $this->onlineMediaHelper = OnlineMediaHelperRegistry::getInstance()->getOnlineMediaHelper($orgFile);
74 } else {
75 $this->onlineMediaHelper = false;
76 }
77 }
78 return $this->onlineMediaHelper;
79 }
80
81 /**
82 * Render for given File(Reference) html output
83 *
84 * @param FileInterface $file
85 * @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
86 * @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
87 * @param array $options
88 * @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
89 * @return string
90 */
91 public function render(FileInterface $file, $width, $height, array $options = null, $usedPathsRelativeToCurrentScript = false)
92 {
93 // Check for an autoplay option at the file reference itself, if not overridden yet.
94 if (!isset($options['autoplay']) && $file instanceof FileReference) {
95 $autoplay = $file->getProperty('autoplay');
96 if ($autoplay !== null) {
97 $options['autoplay'] = $autoplay;
98 }
99 }
100
101 $urlParams = [];
102 if (!empty($options['autoplay'])) {
103 $urlParams[] = 'autoplay=1';
104 }
105 if (!empty($options['loop'])) {
106 $urlParams[] = 'loop=1';
107 }
108 $urlParams[] = 'title=' . (int)!empty($options['showinfo']);
109 $urlParams[] = 'byline=' . (int)!empty($options['showinfo']);
110 $urlParams[] = 'portrait=0';
111
112 if ($file instanceof FileReference) {
113 $orgFile = $file->getOriginalFile();
114 } else {
115 $orgFile = $file;
116 }
117
118 $videoId = $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
119 $src = sprintf('https://player.vimeo.com/video/%s?%s', $videoId, implode('&amp;', $urlParams));
120
121 $attributes = ['allowfullscreen'];
122 if (is_array($options['additionalAttributes'])) {
123 $attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
124 }
125 if (is_array($options['data'])) {
126 array_walk($options['data'], function (&$value, $key) {
127 $value = 'data-' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
128 });
129 $attributes[] = implode(' ', $options['data']);
130 }
131 if ((int)$width > 0) {
132 $attributes[] = 'width="' . (int)$width . '"';
133 }
134 if ((int)$height > 0) {
135 $attributes[] = 'height="' . (int)$height . '"';
136 }
137 if (is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5') {
138 $attributes[] = 'frameborder="0"';
139 }
140 foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick'] as $key) {
141 if (!empty($options[$key])) {
142 $attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
143 }
144 }
145
146 return sprintf(
147 '<iframe src="%s"%s></iframe>',
148 $src,
149 empty($attributes) ? '' : ' ' . implode(' ', $attributes)
150 );
151 }
152 }