Skip to content

Commit

Permalink
Merge pull request #35 from ErHaWeb/main
Browse files Browse the repository at this point in the history
[FEATURE] Add structured data for videos
  • Loading branch information
sfroemkenjw committed May 3, 2023
2 parents 328c9b8 + c96e8c1 commit 80ee82b
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 5 deletions.
62 changes: 62 additions & 0 deletions Classes/ViewHelpers/Format/SecondsToISO8601ViewHelper.php
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package jweiland/video-shariff.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace JWeiland\VideoShariff\ViewHelpers\Format;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
* Class SecondsToISO8601ViewHelper
*/
class SecondsToISO8601ViewHelper extends AbstractViewHelper
{
/**
* Returns the absolute web path to the preview image.
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
* @throws \UnexpectedValueException
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
): string {
$seconds = (int)$renderChildrenClosure();
$intervals = [
'D' => 60 * 60 * 24,
'H' => 60 * 60,
'M' => 60,
'S' => 1,
];

$pt = 'P';
$result = '';
foreach ($intervals as $tag => $divisor) {
$qty = floor($seconds / $divisor);
if (!$qty && $result === '') {
$pt = 'T';
continue;
}
$seconds -= $qty * $divisor;
$result .= $qty . $tag;
}

if ($result === '') {
$result = '0S';
}

return $pt . $result;
}
}
75 changes: 75 additions & 0 deletions Classes/ViewHelpers/VideoCreationDateViewHelper.php
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package jweiland/video-shariff.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace JWeiland\VideoShariff\ViewHelpers;

use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
use TYPO3\CMS\Extbase\Domain\Model\FileReference as ExtbaseFileReference;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
* Class VideoCreationDateViewHelper
*/
class VideoCreationDateViewHelper extends AbstractViewHelper
{
/**
* Initialize arguments
*/
public function initializeArguments(): void
{
$this->registerArgument(
'fileReference',
'object',
'FileReference to be used for creating the preview image'
);
}

/**
* Returns the absolute web path to the preview image.
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return int
* @throws \UnexpectedValueException
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
): int {
/** @var FileReference|ExtbaseFileReference $fileReference */
$fileReference = $arguments['fileReference'];

// get Resource Object (non ExtBase version)
if (is_callable([$fileReference, 'getOriginalResource'])) {
// We have a domain model, so we need to fetch the FAL resource object from there
$fileReference = $fileReference->getOriginalResource();
}
if (!($fileReference instanceof FileInterface || $fileReference instanceof AbstractFileFolder)) {
throw new \UnexpectedValueException('Supplied file object type ' . get_class($fileReference) . ' must be FileInterface or AbstractFileFolder.', 1454252193);
}
$file = $fileReference->getOriginalFile();
if ($file->getProperty('content_creation_date')) {
return $file->getProperty('content_creation_date');
}
if ($file->getProperty('creation_date')) {
return $file->getProperty('creation_date');
}
if ($file->getProperty('crdate')) {
return $file->getProperty('crdate');
}
return 0;
}
}
73 changes: 73 additions & 0 deletions Classes/ViewHelpers/VideoPublicUrlViewHelper.php
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package jweiland/video-shariff.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace JWeiland\VideoShariff\ViewHelpers;

use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
use TYPO3\CMS\Extbase\Domain\Model\FileReference as ExtbaseFileReference;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
* Class VideoPublicUrlViewHelper
*/
class VideoPublicUrlViewHelper extends AbstractViewHelper
{
/**
* Initialize arguments
*/
public function initializeArguments(): void
{
$this->registerArgument(
'fileReference',
'object',
'FileReference to be used for creating the preview image'
);
}

/**
* Returns the absolute web path to the preview image.
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
* @throws \UnexpectedValueException
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
): string {
/** @var FileReference|ExtbaseFileReference $fileReference */
$fileReference = $arguments['fileReference'];

// get Resource Object (non ExtBase version)
if (is_callable([$fileReference, 'getOriginalResource'])) {
// We have a domain model, so we need to fetch the FAL resource object from there
$fileReference = $fileReference->getOriginalResource();
}
if (!($fileReference instanceof FileInterface || $fileReference instanceof AbstractFileFolder)) {
throw new \UnexpectedValueException('Supplied file object type ' . get_class($fileReference) . ' must be FileInterface or AbstractFileFolder.', 1454252193);
}
$file = $fileReference->getOriginalFile();
$helper = OnlineMediaHelperRegistry::getInstance()->getOnlineMediaHelper($file);
if ($helper) {
$publicUrl = $helper->getPublicUrl($file);
} else {
$publicUrl = '';
}
return $publicUrl;
}
}
Expand Up @@ -4,17 +4,20 @@
data-namespace-typo3-fluid="true">

<f:variable name="previewImage" value="{jw:videoPreviewImage(fileReference: '{file}')}"/>
<f:variable name="publicUrl" value="{jw:videoPublicUrl(fileReference: '{file}')}" />
<f:variable name="creationDate" value="{jw:videoCreationDate(fileReference: '{file}')}" />
<f:variable name="playerHtml"><f:render partial="Media/Rendering/Video" arguments="{file: file, dimensions: dimensions, settings: settings}"/></f:variable>

<figure class="video">
<figure class="video" itemprop="video" itemscope itemtype="https://schema.org/VideoObject">
<div class="video-embed">
<f:if condition="{previewImage}">
<f:then>
<a href="#play" class="video-shariff-play" data-video="{playerHtml -> f:format.json()}">
<f:image src="{previewImage}"
width="{dimensions.width}"
height="{dimensions.height}"
alt="{file.title}"/>
alt="{file.title}"
additionalAttributes="{itemprop:'thumbnailUrl'}"/>
<span class="video-shariff-preview-overlay"></span>
<div class="video-shariff-preview">
<span class="video-shariff-preview-icon"></span>
Expand All @@ -29,9 +32,50 @@
</f:if>
</div>
<f:if condition="{file.description}">
<figcaption class="video-caption">
{file.description}
</figcaption>
<f:then>
<figcaption class="video-caption" itemprop="description">
{file.description}
</figcaption>
</f:then>
<f:else>
<f:if condition="{file.properties.caption}">
<f:then>
<meta itemprop="description" content="{file.properties.caption}" />
</f:then>
<f:else>
<f:if condition="{file.title}">
<f:then>
<meta itemprop="description" content="{file.title}" />
</f:then>
<f:else>
<meta itemprop="description" content="{file.name}" />
</f:else>
</f:if>
</f:else>
</f:if>
</f:else>
</f:if>
<f:if condition="{file.title}">
<f:then>
<meta itemprop="name" content="{file.title}" />
</f:then>
<f:else>
<meta itemprop="name" content="{file.name}" />
</f:else>
</f:if>
<f:if condition="{publicUrl}">
<f:then>
<meta itemprop="embedUrl" content="{publicUrl}" />
</f:then>
<f:else>
<meta itemprop="contentUrl" content="{file.publicUrl}" />
</f:else>
</f:if>
<f:if condition="{creationDate}">
<meta itemprop="uploadDate" content="{creationDate -> f:format.date(format: 'c')}" />
</f:if>
<f:if condition="{file.properties.duration}">
<meta itemprop="duration" content="{file.properties.duration -> jw:format.secondsToISO8601()}" />
</f:if>
</figure>
</html>

0 comments on commit 80ee82b

Please sign in to comment.