[BUGFIX] MediaViewHelper does not render data-focus-area attribute
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / MediaViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
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.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
18 use TYPO3\CMS\Core\Resource\FileInterface;
19 use TYPO3\CMS\Core\Resource\FileReference;
20 use TYPO3\CMS\Core\Resource\Rendering\RendererRegistry;
21 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
22 use TYPO3\CMS\Extbase\Service\ImageService;
23 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
24
25 /**
26 * Render a given media file with the correct html tag.
27 *
28 * It asks the RendererRegister for the correct Renderer class and if not found it falls
29 * back to the ImageViewHelper as that is the "Renderer" class for images in Fluid context.
30 *
31 * = Examples =
32 *
33 * <code title="Image Object">
34 * <f:media file="{file}" width="400" height="375" />
35 * </code>
36 * <output>
37 * <img alt="alt set in image record" src="fileadmin/_processed_/323223424.png" width="396" height="375" />
38 * </output>
39 *
40 * <code title="MP4 Video Object">
41 * <f:media file="{file}" width="400" height="375" />
42 * </code>
43 * <output>
44 * <video width="400" height="375" controls><source src="fileadmin/user_upload/my-video.mp4" type="video/mp4"></video>
45 * </output>
46 *
47 * <code title="MP4 Video Object with loop and autoplay option set">
48 * <f:media file="{file}" width="400" height="375" additionalConfig="{loop: '1', autoplay: '1'}" />
49 * </code>
50 * <output>
51 * <video width="400" height="375" controls loop><source src="fileadmin/user_upload/my-video.mp4" type="video/mp4"></video>
52 * </output>
53 */
54 class MediaViewHelper extends AbstractTagBasedViewHelper
55 {
56 /**
57 * @var string
58 */
59 protected $tagName = 'img';
60
61 /**
62 * Initialize arguments.
63 *
64 * @return void
65 */
66 public function initializeArguments()
67 {
68 parent::initializeArguments();
69 $this->registerUniversalTagAttributes();
70 $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
71 $this->registerArgument('file', 'object', 'File', true);
72 $this->registerArgument('additionalConfig', 'string', 'This array can hold additional configuration that is passed though to the Renderer object', false, []);
73 $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.');
74 $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.');
75 $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
76 }
77
78 /**
79 * Render a given media file
80 *
81 * @return string Rendered tag
82 * @throws \UnexpectedValueException
83 */
84 public function render()
85 {
86 $file = $this->arguments['file'];
87 $additionalConfig = $this->arguments['additionalConfig'];
88 $width = $this->arguments['width'];
89 $height = $this->arguments['height'];
90
91 // get Resource Object (non ExtBase version)
92 if (is_callable([$file, 'getOriginalResource'])) {
93 // We have a domain model, so we need to fetch the FAL resource object from there
94 $file = $file->getOriginalResource();
95 }
96
97 if (!($file instanceof FileInterface || $file instanceof AbstractFileFolder)) {
98 throw new \UnexpectedValueException('Supplied file object type ' . get_class($file) . ' must be FileInterface or AbstractFileFolder.', 1454252193);
99 }
100
101 $fileRenderer = RendererRegistry::getInstance()->getRenderer($file);
102
103 // Fallback to image when no renderer is found
104 if ($fileRenderer === null) {
105 return $this->renderImage($file, $width, $height);
106 } else {
107 $additionalConfig = array_merge_recursive($this->arguments, $additionalConfig);
108 return $fileRenderer->render($file, $width, $height, $additionalConfig);
109 }
110 }
111
112 /**
113 * Render img tag
114 *
115 * @param FileInterface $image
116 * @param string $width
117 * @param string $height
118 * @return string Rendered img tag
119 */
120 protected function renderImage(FileInterface $image, $width, $height)
121 {
122 $cropVariant = $this->arguments['cropVariant'] ?: 'default';
123 $cropString = $image instanceof FileReference ? $image->getProperty('crop') : '';
124 $cropVariantCollection = CropVariantCollection::create((string)$cropString);
125 $cropArea = $cropVariantCollection->getCropArea($cropVariant);
126 $processingInstructions = [
127 'width' => $width,
128 'height' => $height,
129 'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
130 ];
131 $imageService = $this->getImageService();
132 $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
133 $imageUri = $imageService->getImageUri($processedImage);
134
135 if (!$this->tag->hasAttribute('data-focus-area')) {
136 $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
137 if (!$focusArea->isEmpty()) {
138 $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
139 }
140 }
141 $this->tag->addAttribute('src', $imageUri);
142 $this->tag->addAttribute('width', $processedImage->getProperty('width'));
143 $this->tag->addAttribute('height', $processedImage->getProperty('height'));
144
145 $alt = $image->getProperty('alternative');
146 $title = $image->getProperty('title');
147
148 // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
149 if (empty($this->arguments['alt'])) {
150 $this->tag->addAttribute('alt', $alt);
151 }
152 if (empty($this->arguments['title']) && $title) {
153 $this->tag->addAttribute('title', $title);
154 }
155
156 return $this->tag->render();
157 }
158
159 /**
160 * Return an instance of ImageService
161 *
162 * @return ImageService
163 */
164 protected function getImageService()
165 {
166 return $this->objectManager->get(ImageService::class);
167 }
168 }