*
* Output::
*
*
*
* MP4 Video Object
* ----------------
*
* ::
*
*
*
* Output::
*
*
*
* MP4 Video Object with loop and autoplay option set
* --------------------------------------------------
*
* ::
*
*
*
* Output::
*
*
*/
class MediaViewHelper extends AbstractTagBasedViewHelper
{
/**
* @var string
*/
protected $tagName = 'img';
/**
* Initialize arguments.
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
$this->registerArgument('file', 'object', 'File', true);
$this->registerArgument('additionalConfig', 'array', 'This array can hold additional configuration that is passed though to the Renderer object', false, []);
$this->registerArgument('width', 'string', 'This can be a numeric value representing the fixed width of in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
$this->registerArgument('height', 'string', 'This can be a numeric value representing the fixed height in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
$this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
}
/**
* Render a given media file
*
* @return string Rendered tag
* @throws \UnexpectedValueException
*/
public function render()
{
$file = $this->arguments['file'];
$additionalConfig = (array)$this->arguments['additionalConfig'];
$width = $this->arguments['width'];
$height = $this->arguments['height'];
// get Resource Object (non ExtBase version)
if (is_callable([$file, 'getOriginalResource'])) {
// We have a domain model, so we need to fetch the FAL resource object from there
$file = $file->getOriginalResource();
}
if (!($file instanceof FileInterface || $file instanceof AbstractFileFolder)) {
throw new \UnexpectedValueException('Supplied file object type ' . get_class($file) . ' must be FileInterface or AbstractFileFolder.', 1454252193);
}
$fileRenderer = RendererRegistry::getInstance()->getRenderer($file);
// Fallback to image when no renderer is found
if ($fileRenderer === null) {
return $this->renderImage($file, $width, $height);
}
$additionalConfig = array_merge_recursive($this->arguments, $additionalConfig);
return $fileRenderer->render($file, $width, $height, $additionalConfig);
}
/**
* Render img tag
*
* @param FileInterface $image
* @param string $width
* @param string $height
* @return string Rendered img tag
*/
protected function renderImage(FileInterface $image, $width, $height)
{
$cropVariant = $this->arguments['cropVariant'] ?: 'default';
$cropString = $image instanceof FileReference ? $image->getProperty('crop') : '';
$cropVariantCollection = CropVariantCollection::create((string)$cropString);
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
$processingInstructions = [
'width' => $width,
'height' => $height,
'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
];
$imageService = $this->getImageService();
$processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
$imageUri = $imageService->getImageUri($processedImage);
if (!$this->tag->hasAttribute('data-focus-area')) {
$focusArea = $cropVariantCollection->getFocusArea($cropVariant);
if (!$focusArea->isEmpty()) {
$this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
}
}
$this->tag->addAttribute('src', $imageUri);
$this->tag->addAttribute('width', $processedImage->getProperty('width'));
$this->tag->addAttribute('height', $processedImage->getProperty('height'));
$alt = $image->getProperty('alternative');
$title = $image->getProperty('title');
// The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
if (empty($this->arguments['alt'])) {
$this->tag->addAttribute('alt', $alt);
}
if (empty($this->arguments['title']) && $title) {
$this->tag->addAttribute('title', $title);
}
return $this->tag->render();
}
/**
* Return an instance of ImageService
*
* @return ImageService
*/
protected function getImageService()
{
return GeneralUtility::makeInstance(ImageService::class);
}
}