1<?php
2declare(strict_types = 1);
3namespace TYPO3\CMS\Core\ViewHelpers;
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
18use TYPO3\CMS\Core\Imaging\Icon;
19use TYPO3\CMS\Core\Imaging\IconFactory;
20use TYPO3\CMS\Core\Resource\ResourceInterface;
21use TYPO3\CMS\Core\Utility\GeneralUtility;
22use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
24use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
25
26/**
27 * Displays icon for a FAL resource (file or folder means a :php:`TYPO3\CMS\Core\Resource\ResourceInterface`).
28 *
29 * Examples
30 * ========
31 *
32 * Default::
33 *
34 *    <core:iconForResource resource="{file.resource}" />
35 *
36 * Output::
37 *
38 *     <span class="t3js-icon icon icon-size-small icon-state-default icon-mimetypes-text-html" data-identifier="mimetypes-text-html">
39 *         <span class="icon-markup">
40 *             <img src="/typo3/sysext/core/Resources/Public/Icons/T3Icons/mimetypes/mimetypes-text-html.svg" width="16" height="16">
41 *         </span>
42 *     </span>
43 */
44class IconForResourceViewHelper extends AbstractViewHelper
45{
46    use CompileWithRenderStatic;
47
48    /**
49     * ViewHelper returns HTML, thus we need to disable output escaping
50     *
51     * @var bool
52     */
53    protected $escapeOutput = false;
54
55    /**
56     * Initializes the arguments
57     */
58    public function initializeArguments()
59    {
60        $this->registerArgument('resource', ResourceInterface::class, 'Resource', true);
61        $this->registerArgument('size', 'string', 'The icon size', false, Icon::SIZE_SMALL);
62        $this->registerArgument('overlay', 'string', 'Overlay identifier', false, null);
63        $this->registerArgument('options', 'array', 'An associative array with additional options', false, []);
64        $this->registerArgument('alternativeMarkupIdentifier', 'string', 'Alternative markup identifier', false, null);
65    }
66
67    /**
68     * @param array $arguments
69     * @param \Closure $renderChildrenClosure
70     * @param RenderingContextInterface $renderingContext
71     * @return string
72     */
73    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
74    {
75        $resource = $arguments['resource'];
76        $size = $arguments['size'];
77        $overlay = $arguments['overlay'];
78        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
79        return $iconFactory->getIconForResource($resource, $size, $overlay, $arguments['options'])->render($arguments['alternativeMarkupIdentifier']);
80    }
81}
82