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