|
<?php |
|
|
|
declare(strict_types=1); |
|
|
|
namespace Dmind\ConfigureCookieman\Rendering; |
|
|
|
use Dmind\ConfigureCookieman\Service\YoutubeImageService; |
|
use Psr\Http\Message\ServerRequestInterface; |
|
use TYPO3\CMS\Core\Http\ApplicationType; |
|
use TYPO3\CMS\Core\Localization\LanguageService; |
|
use TYPO3\CMS\Core\Localization\LanguageServiceFactory; |
|
use TYPO3\CMS\Core\Resource\FileInterface; |
|
use TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface; |
|
use TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer; |
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
|
|
class CookiemanYoutubeRenderer extends YouTubeRenderer implements FileRendererInterface |
|
{ |
|
protected ServerRequestInterface $request; |
|
protected ?LanguageService $languageService = null; |
|
|
|
public function __construct() |
|
{ |
|
$this->request = $GLOBALS['TYPO3_REQUEST']; |
|
|
|
if (!$this->isBackend()) { |
|
$this->languageService = GeneralUtility::makeInstance(LanguageServiceFactory::class) |
|
->createFromSiteLanguage( |
|
$this->request->getAttribute('language') |
|
); |
|
} |
|
} |
|
|
|
public function getPriority(): int |
|
{ |
|
return 10; |
|
} |
|
|
|
public function render( |
|
FileInterface $file, |
|
$width, |
|
$height, |
|
array $options = [] |
|
): string { |
|
$options = $this->collectOptions($options, $file); |
|
$src = $this->createYouTubeUrl($options, $file); |
|
$attributes = $this->collectIframeAttributes($width, $height, $options); |
|
|
|
$imageService = GeneralUtility::makeInstance(YoutubeImageService::class); |
|
$previewUrl = $imageService->getPreviewImage($this->getVideoIdFromFile($file)); |
|
$previewHtml = $previewUrl ? '<img src="' . $previewUrl . '" class="img-fluid">' : ''; |
|
|
|
if ($this->isBackend()) { |
|
return $previewHtml; |
|
} |
|
|
|
$bannerText = $this->sL( |
|
'LLL:EXT:configure_cookieman/Resources/Private/Language/locallang.xlf:banner.YouTube.text' |
|
); |
|
$buttonText = $this->sL( |
|
'LLL:EXT:configure_cookieman/Resources/Private/Language/locallang.xlf:banner.YouTube.buttonText' |
|
); |
|
|
|
return sprintf( |
|
' |
|
<div class="cookieman-video-youtube"> |
|
' . $previewHtml . ' |
|
<div class="cookieman-video-banner"> |
|
<div class="cookieman-video-banner-text"> |
|
<p>' . $bannerText . '</p> |
|
<p><button class="btn btn-primary text-grey bg-white p-4 rounded-lg">' . $buttonText . '</button></p> |
|
</div> |
|
</div> |
|
update <iframe hidden data-src="%s"%s></iframe> |
|
</div> |
|
', |
|
htmlspecialchars($src, ENT_QUOTES | ENT_HTML5), |
|
empty($attributes) ? '' : ' ' . $this->implodeAttributes($attributes) |
|
); |
|
} |
|
|
|
private function isBackend(): bool |
|
{ |
|
return ApplicationType::fromRequest($this->request)->isBackend(); |
|
} |
|
|
|
private function sL(string $string): string |
|
{ |
|
return $this->languageService?->sL($string) ?? $string; |
|
} |
|
} |