2 namespace TYPO3\CMS\Core\Resource\Rendering
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
16 use TYPO3\CMS\Core\Resource\File
;
17 use TYPO3\CMS\Core\Resource\FileInterface
;
18 use TYPO3\CMS\Core\Resource\FileReference
;
19 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface
;
20 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
;
21 use TYPO3\CMS\Core\Utility\GeneralUtility
;
24 * YouTube renderer class
26 class YouTubeRenderer
implements FileRendererInterface
29 * @var OnlineMediaHelperInterface
31 protected $onlineMediaHelper;
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
42 public function getPriority()
48 * Check if given File(Reference) can be rendered
50 * @param FileInterface $file File of FileReference to render
53 public function canRender(FileInterface
$file)
55 return ($file->getMimeType() === 'video/youtube' ||
$file->getExtension() === 'youtube') && $this->getOnlineMediaHelper($file) !== false
;
59 * Get online media helper
61 * @param FileInterface $file
62 * @return bool|OnlineMediaHelperInterface
64 protected function getOnlineMediaHelper(FileInterface
$file)
66 if ($this->onlineMediaHelper
=== null
) {
68 if ($orgFile instanceof FileReference
) {
69 $orgFile = $orgFile->getOriginalFile();
71 if ($orgFile instanceof File
) {
72 $this->onlineMediaHelper
= OnlineMediaHelperRegistry
::getInstance()->getOnlineMediaHelper($orgFile);
74 $this->onlineMediaHelper
= false
;
77 return $this->onlineMediaHelper
;
81 * Render for given File(Reference) html output
83 * @param FileInterface $file
84 * @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
85 * @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
86 * @param array $options
87 * @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
90 public function render(FileInterface
$file, $width, $height, array $options = null
, $usedPathsRelativeToCurrentScript = false
)
92 // Check for an autoplay option at the file reference itself, if not overriden yet.
93 if (!isset($options['autoplay']) && $file instanceof FileReference
) {
94 $autoplay = $file->getProperty('autoplay');
95 if ($autoplay !== null
) {
96 $options['autoplay'] = $autoplay;
100 if ($file instanceof FileReference
) {
101 $orgFile = $file->getOriginalFile();
106 $videoId = $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
108 $urlParams = ['autohide=1'];
109 if (!isset($options['controls']) ||
!empty($options['controls'])) {
110 $urlParams[] = 'controls=2';
112 if (!empty($options['autoplay'])) {
113 $urlParams[] = 'autoplay=1';
115 if (!empty($options['loop'])) {
116 $urlParams[] = 'loop=1&playlist=' . $videoId;
118 if (isset($options['relatedVideos'])) {
119 $urlParams[] = 'rel=' . (int)(bool
)$options['relatedVideos'];
121 if (!isset($options['enablejsapi']) ||
!empty($options['enablejsapi'])) {
122 $urlParams[] = 'enablejsapi=1&origin=' . GeneralUtility
::getIndpEnv('TYPO3_REQUEST_HOST');
124 $urlParams[] = 'showinfo=' . (int)!empty($options['showinfo']);
127 'https://www.youtube%s.com/embed/%s?%s',
128 !empty($options['no-cookie']) ?
'-nocookie' : '',
130 implode('&', $urlParams)
133 $attributes = ['allowfullscreen'];
134 if ((int)$width > 0) {
135 $attributes[] = 'width="' . (int)$width . '"';
137 if ((int)$height > 0) {
138 $attributes[] = 'height="' . (int)$height . '"';
140 if (is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->config
['config']['doctype'] !== 'html5') {
141 $attributes[] = 'frameborder="0"';
143 foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload'] as $key) {
144 if (!empty($options[$key])) {
145 $attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
150 '<iframe src="%s"%s></iframe>',
152 empty($attributes) ?
'' : ' ' . implode(' ', $attributes)