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