1<?php
2namespace TYPO3\CMS\Core\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
17use TYPO3\CMS\Core\Imaging\Icon;
18use TYPO3\CMS\Core\Imaging\IconFactory;
19use TYPO3\CMS\Core\Type\Icon\IconState;
20use TYPO3\CMS\Core\Utility\GeneralUtility;
21use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24
25/**
26 * Displays icon identified by icon identifier.
27 *
28 * Examples
29 * ========
30 *
31 * Default::
32 *
33 *    <core:icon identifier="actions-menu" />
34 *
35 * Output::
36 *
37 *     <span class="t3js-icon icon icon-size-small icon-state-default icon-actions-menu" data-identifier="actions-menu">
38 *         <span class="icon-markup">
39 *             <img src="/typo3/sysext/core/Resources/Public/Icons/T3Icons/actions/actions-menu.svg" width="16" height="16">
40 *         </span>
41 *     </span>
42 *
43 * Inline::
44 *
45 *    <core:icon identifier="actions-menu" alternativeMarkupIdentifier="inline" />
46 *
47 * Output::
48 *
49 *     <span class="t3js-icon icon icon-size-small icon-state-default icon-actions-menu" data-identifier="actions-menu">
50 *         <span class="icon-markup">
51 *             <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><g class="icon-color"><path d="M9 12v2H7v-2h2m.5-1h-3c-.3 0-.5.2-.5.5v3c0 .3.2.5.5.5h3c.3 0 .5-.2.5-.5v-3c0-.3-.2-.5-.5-.5zM9 7v2H7V7h2m.5-1h-3c-.3 0-.5.2-.5.5v3c0 .3.2.5.5.5h3c.3 0 .5-.2.5-.5v-3c0-.3-.2-.5-.5-.5zM9 2v2H7V2h2m.5-1h-3c-.3 0-.5.2-.5.5v3c0 .3.2.5.5.5h3c.3 0 .5-.2.5-.5v-3c0-.3-.2-.5-.5-.5zM4 7v2H2V7h2m.5-1h-3c-.3 0-.5.2-.5.5v3c0 .3.2.5.5.5h3c.3 0 .5-.2.5-.5v-3c0-.3-.2-.5-.5-.5zM4 2v2H2V2h2m.5-1h-3c-.3 0-.5.2-.5.5v3c0 .3.2.5.5.5h3c.3 0 .5-.2.5-.5v-3c0-.3-.2-.5-.5-.5zM4 12v2H2v-2h2m.5-1h-3c-.3 0-.5.2-.5.5v3c0 .3.2.5.5.5h3c.3 0 .5-.2.5-.5v-3c0-.3-.2-.5-.5-.5zM14 7v2h-2V7h2m.5-1h-3c-.3 0-.5.2-.5.5v3c0 .3.2.5.5.5h3c.3 0 .5-.2.5-.5v-3c0-.3-.2-.5-.5-.5zM14 2v2h-2V2h2m.5-1h-3c-.3 0-.5.2-.5.5v3c0 .3.2.5.5.5h3c.3 0 .5-.2.5-.5v-3c0-.3-.2-.5-.5-.5zM14 12v2h-2v-2h2m.5-1h-3c-.3 0-.5.2-.5.5v3c0 .3.2.5.5.5h3c.3 0 .5-.2.5-.5v-3c0-.3-.2-.5-.5-.5z"/></g></svg>
52 *         </span>
53 *     </span>
54 */
55class IconViewHelper extends AbstractViewHelper
56{
57    use CompileWithRenderStatic;
58
59    /**
60     * ViewHelper returns HTML, thus we need to disable output escaping
61     *
62     * @var bool
63     */
64    protected $escapeOutput = false;
65
66    /**
67     * Initializes the arguments
68     */
69    public function initializeArguments()
70    {
71        $this->registerArgument('identifier', 'string', 'Identifier of the icon as registered in the Icon Registry.', true);
72        $this->registerArgument('size', 'string', 'Desired size of the icon. All values of the Icons.sizes enum are allowed, these are: "small", "default", "large" and "overlay".', false, Icon::SIZE_SMALL);
73        $this->registerArgument('overlay', 'string', 'Identifier of an overlay icon as registered in the Icon Registry.', false, null);
74        $this->registerArgument('state', 'string', 'Sets the state of the icon. All values of the Icons.states enum are allowed, these are: "default" and "disabled".', false, IconState::STATE_DEFAULT);
75        $this->registerArgument('alternativeMarkupIdentifier', 'string', 'Alternative icon identifier. Takes precedence over the identifier if supported by the IconProvider.', false, null);
76    }
77
78    /**
79     * Prints icon html for $identifier key
80     *
81     * @param array $arguments
82     * @param \Closure $renderChildrenClosure
83     * @param RenderingContextInterface $renderingContext
84     * @return string
85     */
86    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
87    {
88        $identifier = $arguments['identifier'];
89        $size = $arguments['size'];
90        $overlay = $arguments['overlay'];
91        $state = IconState::cast($arguments['state']);
92        $alternativeMarkupIdentifier = $arguments['alternativeMarkupIdentifier'];
93        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
94        return $iconFactory->getIcon($identifier, $size, $overlay, $state)->render($alternativeMarkupIdentifier);
95    }
96}
97