1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18namespace TYPO3\CMS\Core\ViewHelpers;
19
20use TYPO3\CMS\Core\Imaging\Icon;
21use TYPO3\CMS\Core\Imaging\IconFactory;
22use TYPO3\CMS\Core\Resource\ResourceInterface;
23use TYPO3\CMS\Core\Utility\GeneralUtility;
24use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
25use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
26use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
27
28/**
29 * Displays icon for a FAL resource (file or folder means a :php:`TYPO3\CMS\Core\Resource\ResourceInterface`).
30 *
31 * Examples
32 * ========
33 *
34 * Default::
35 *
36 *    <core:iconForResource resource="{file.resource}" />
37 *
38 * Output::
39 *
40 *     <span class="t3js-icon icon icon-size-small icon-state-default icon-mimetypes-text-html" data-identifier="mimetypes-text-html">
41 *         <span class="icon-markup">
42 *             <img src="/typo3/sysext/core/Resources/Public/Icons/T3Icons/mimetypes/mimetypes-text-html.svg" width="16" height="16">
43 *         </span>
44 *     </span>
45 */
46class IconForResourceViewHelper extends AbstractViewHelper
47{
48    use CompileWithRenderStatic;
49
50    /**
51     * ViewHelper returns HTML, thus we need to disable output escaping
52     *
53     * @var bool
54     */
55    protected $escapeOutput = false;
56
57    /**
58     * Initializes the arguments
59     */
60    public function initializeArguments()
61    {
62        $this->registerArgument('resource', ResourceInterface::class, 'Resource', true);
63        $this->registerArgument('size', 'string', 'The icon size', false, Icon::SIZE_SMALL);
64        $this->registerArgument('overlay', 'string', 'Overlay identifier', false, null);
65        $this->registerArgument('options', 'array', 'An associative array with additional options', false, []);
66        $this->registerArgument('alternativeMarkupIdentifier', 'string', 'Alternative markup identifier', false, null);
67    }
68
69    /**
70     * @param array $arguments
71     * @param \Closure $renderChildrenClosure
72     * @param RenderingContextInterface $renderingContext
73     * @return string
74     */
75    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
76    {
77        $resource = $arguments['resource'];
78        $size = $arguments['size'];
79        $overlay = $arguments['overlay'];
80        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
81        return $iconFactory->getIconForResource($resource, $size, $overlay, $arguments['options'])->render($arguments['alternativeMarkupIdentifier']);
82    }
83}
84