Skip to content

Commit

Permalink
[TASK] handle video and audio elements in ce (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszuznanski authored and tmotyl committed Oct 25, 2019
1 parent 0bc5bf9 commit 9a98e95
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 17 deletions.
90 changes: 90 additions & 0 deletions Classes/Resource/Rendering/AudioTagRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/***
*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*
* (c) 2019
*
***/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Resource\Rendering;

use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Audio tag renderer class
*/
class AudioTagRenderer extends \TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer
{
/**
* @return int
*/
public function getPriority(): int
{
return 2;
}

/**
* Render for given File(Reference) html output
*
* @param FileInterface $file
* @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
* @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
* @param array $options
* @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
* @return string
*/
public function render(FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
{
if ($options['returnUrl'] === true) {

// If autoplay isn't set manually check if $file is a FileReference take autoplay from there
if (!isset($options['autoplay']) && $file instanceof FileReference) {
$autoplay = $file->getProperty('autoplay');
if ($autoplay !== null) {
$options['autoplay'] = $autoplay;
}
}

$additionalAttributes = [];
if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
$additionalAttributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
}
if (isset($options['data']) && is_array($options['data'])) {
array_walk($options['data'], function (&$value, $key) {
$value = 'data-' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
});
$additionalAttributes[] = implode(' ', $options['data']);
}
if (!isset($options['controls']) || !empty($options['controls'])) {
$additionalAttributes[] = 'controls';
}
if (!empty($options['autoplay'])) {
$additionalAttributes[] = 'autoplay';
}
if (!empty($options['muted'])) {
$additionalAttributes[] = 'muted';
}
if (!empty($options['loop'])) {
$additionalAttributes[] = 'loop';
}
foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'preload', 'controlsList'] as $key) {
if (!empty($options[$key])) {
$additionalAttributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
}
}

return htmlspecialchars(GeneralUtility::makeInstance(FileUtility::class)->getAbsoluteUrl($file->getPublicUrl($usedPathsRelativeToCurrentScript)), ENT_QUOTES | ENT_HTML5);
} else {
return parent::render(...func_get_args());
}
}
}
99 changes: 99 additions & 0 deletions Classes/Resource/Rendering/VideoTagRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/***
*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*
* (c) 2019
*
***/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Resource\Rendering;

use FriendsOfTYPO3\Headless\Utility\FileUtility;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Video tag renderer class
*/
class VideoTagRenderer extends \TYPO3\CMS\Core\Resource\Rendering\VideoTagRenderer
{
/**
* @return int
*/
public function getPriority(): int
{
return 2;
}

/**
* Render for given File(Reference) html output
*
* @param FileInterface $file
* @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
* @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
* @param array $options
* @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
* @return string
*/
public function render(FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
{
if ($options['returnUrl'] === true) {

// If autoplay isn't set manually check if $file is a FileReference take autoplay from there
if (!isset($options['autoplay']) && $file instanceof FileReference) {
$autoplay = $file->getProperty('autoplay');
if ($autoplay !== null) {
$options['autoplay'] = $autoplay;
}
}

$attributes = [];
if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
$attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
}
if (isset($options['data']) && is_array($options['data'])) {
array_walk($options['data'], function (&$value, $key) {
$value = 'data-' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
});
$attributes[] = implode(' ', $options['data']);
}
if ((int)$width > 0) {
$attributes[] = 'width="' . (int)$width . '"';
}
if ((int)$height > 0) {
$attributes[] = 'height="' . (int)$height . '"';
}
if (!isset($options['controls']) || !empty($options['controls'])) {
$attributes[] = 'controls';
}
if (!empty($options['autoplay'])) {
$attributes[] = 'autoplay';
}
if (!empty($options['muted'])) {
$attributes[] = 'muted';
}
if (!empty($options['loop'])) {
$attributes[] = 'loop';
}
if (isset($options['additionalConfig']) && is_array($options['additionalConfig'])) {
foreach ($options['additionalConfig'] as $key => $value) {
if ((bool)$value) {
$attributes[] = htmlspecialchars($key);
}
}
}

return htmlspecialchars(GeneralUtility::makeInstance(FileUtility::class)->getAbsoluteUrl($file->getPublicUrl($usedPathsRelativeToCurrentScript)), ENT_QUOTES | ENT_HTML5);
} else {
return parent::render(...func_get_args());
}
}
}
34 changes: 17 additions & 17 deletions Classes/Utility/FileUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ public function processImageFile($image, array $dimensions = []): ProcessedFile
}
}

/**
* @param string $fileUrl
* @return string
*/
public function getAbsoluteUrl(string $fileUrl): string
{
$siteUrl = $this->getNormalizedParams()->getSiteUrl();
$sitePath = str_replace($this->getNormalizedParams()->getRequestHost(), '', $siteUrl);
$absoluteUrl = trim($fileUrl);
if (strtolower(substr($absoluteUrl, 0, 4)) !== 'http') {
$fileUrl = preg_replace('#^' . preg_quote($sitePath, '#') . '#', '', $fileUrl);
$fileUrl = $siteUrl . $fileUrl;
}

return $fileUrl;
}

/**
* When retrieving the height or width for a media file
* a possible cropping needs to be taken into account.
Expand Down Expand Up @@ -164,23 +181,6 @@ protected function calculateKilobytesToFileSize(?int $value): string
return number_format(round($bytes, 4 * 2)) . ' ' . $units[$pow];
}

/**
* @param string $fileUrl
* @return string
*/
protected function getAbsoluteUrl(string $fileUrl): string
{
$siteUrl = $this->getNormalizedParams()->getSiteUrl();
$sitePath = str_replace($this->getNormalizedParams()->getRequestHost(), '', $siteUrl);
$absoluteUrl = trim($fileUrl);
if (strtolower(substr($absoluteUrl, 0, 4)) !== 'http') {
$fileUrl = preg_replace('#^' . preg_quote($sitePath, '#') . '#', '', $fileUrl);
$fileUrl = $siteUrl . $fileUrl;
}

return $fileUrl;
}

/**
* @return NormalizedParams
*/
Expand Down
2 changes: 2 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function () {
$rendererRegistry = \TYPO3\CMS\Core\Resource\Rendering\RendererRegistry::getInstance();
$rendererRegistry->registerRendererClass(\FriendsOfTYPO3\Headless\Resource\Rendering\YouTubeRenderer::class);
$rendererRegistry->registerRendererClass(\FriendsOfTYPO3\Headless\Resource\Rendering\VimeoRenderer::class);
$rendererRegistry->registerRendererClass(\FriendsOfTYPO3\Headless\Resource\Rendering\AudioTagRenderer::class);
$rendererRegistry->registerRendererClass(\FriendsOfTYPO3\Headless\Resource\Rendering\VideoTagRenderer::class);
unset($rendererRegistry);
}
);

0 comments on commit 9a98e95

Please sign in to comment.