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