1<?php
2declare(strict_types = 1);
3namespace TYPO3\CMS\Backend\Form\FieldControl;
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\Backend\Form\AbstractNode;
19use TYPO3\CMS\Core\Utility\GeneralUtility;
20use TYPO3\CMS\Core\Utility\StringUtility;
21
22/**
23 * "Reset selection to previous selected items" icon,
24 * typically used by type=select with renderType=selectSingleBox
25 */
26class ResetSelection extends AbstractNode
27{
28    /**
29     * Add button control
30     *
31     * @return array As defined by FieldControl class
32     */
33    public function render()
34    {
35        $parameterArray = $this->data['parameterArray'];
36        $itemName = $parameterArray['itemFormElName'];
37
38        $selectItems = $parameterArray['fieldConf']['config']['items'];
39        $itemArray = array_flip($parameterArray['itemFormElValue']);
40        $initiallySelectedIndices = [];
41        foreach ($selectItems as $i => $item) {
42            $value = $item[1];
43            // Selected or not by default
44            if (isset($itemArray[$value])) {
45                $initiallySelectedIndices[] = $i;
46            }
47        }
48
49        $id = StringUtility::getUniqueId('t3js-formengine-fieldcontrol-');
50
51        return [
52            'iconIdentifier' => 'actions-edit-undo',
53            'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.revertSelection',
54            'linkAttributes' => [
55                'id' => htmlspecialchars($id),
56                'data-item-name' => htmlspecialchars($itemName),
57                'data-selected-indices' => json_encode($initiallySelectedIndices),
58            ],
59            'requireJsModules' => [
60                ['TYPO3/CMS/Backend/FormEngine/FieldControl/ResetSelection' => 'function(FieldControl) {new FieldControl(' . GeneralUtility::quoteJSvalue('#' . $id) . ');}'],
61            ],
62        ];
63    }
64}
65