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