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\Utility\GeneralUtility;
20use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24/**
25 * Displays icon for record.
26 *
27 * Examples
28 * ========
29 *
30 * Default::
31 *
32 *    <core:iconForRecord table="tt_content" row="{record}" />
33 *
34 * Output::
35 *
36 *     <span class="t3js-icon icon icon-size-small icon-state-default icon-mimetypes-x-content-text" data-identifier="mimetypes-x-content-text">
37 *         <span class="icon-markup">
38 *             <img src="/typo3/sysext/core/Resources/Public/Icons/T3Icons/mimetypes/mimetypes-x-content-text.svg" width="16" height="16">
39 *         </span>
40 *     </span>
41 */
42class IconForRecordViewHelper extends AbstractViewHelper
43{
44    use CompileWithRenderStatic;
45
46    /**
47     * ViewHelper returns HTML, thus we need to disable output escaping
48     *
49     * @var bool
50     */
51    protected $escapeOutput = false;
52
53    /**
54     * Initializes the arguments
55     */
56    public function initializeArguments()
57    {
58        $this->registerArgument('table', 'string', 'the table for the record icon', true);
59        $this->registerArgument('row', 'array', 'the record row', true);
60        $this->registerArgument('size', 'string', 'the icon size', false, Icon::SIZE_SMALL);
61        $this->registerArgument('alternativeMarkupIdentifier', 'string', 'alternative markup identifier', false, null);
62    }
63
64    /**
65     * @param array $arguments
66     * @param \Closure $renderChildrenClosure
67     * @param RenderingContextInterface $renderingContext
68     * @return string
69     */
70    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
71    {
72        $table = $arguments['table'];
73        $size = $arguments['size'];
74        $row = $arguments['row'];
75        $alternativeMarkupIdentifier = $arguments['alternativeMarkupIdentifier'];
76        /** @var IconFactory $iconFactory */
77        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
78        return $iconFactory->getIconForRecord($table, $row, $size)->render($alternativeMarkupIdentifier);
79    }
80}
81