[BUGFIX] Show active state for SelectIcons
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FieldWizard / SelectIcons.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Form\FieldWizard;
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\Backend\Form\Utility\FormEngineUtility;
20
21 /**
22 * Render thumbnails of icons,
23 * typically used with type=select.
24 */
25 class SelectIcons extends AbstractNode
26 {
27 /**
28 * Render thumbnails of selected files
29 *
30 * @return array
31 */
32 public function render(): array
33 {
34 $result = $this->initializeResultArray();
35
36 $parameterArray = $this->data['parameterArray'];
37 $selectItems = $parameterArray['fieldConf']['config']['items'];
38
39 $selectItemCounter = 0;
40 foreach ($selectItems as $item) {
41 if ($item[1] === '--div--') {
42 continue;
43 }
44 $icon = !empty($item[2]) ? FormEngineUtility::getIconHtml($item[2], $item[0], $item[0]) : '';
45 if ($icon) {
46 $fieldValue = $this->data['databaseRow'][$this->data['fieldName']];
47 $selectIcons[] = [
48 'title' => $item[0],
49 'active' => ($fieldValue[0] === (string)$item[1]) ? true : false,
50 'icon' => $icon,
51 'index' => $selectItemCounter,
52 ];
53 }
54 $selectItemCounter++;
55 }
56
57 $html = [];
58 if (!empty($selectIcons)) {
59 $html[] = '<div class="t3js-forms-select-single-icons icon-list">';
60 $html[] = '<div class="row">';
61 foreach ($selectIcons as $i => $selectIcon) {
62 $active = ($selectIcon['active']) ? ' active' : '';
63 $html[] = '<div class="item' . $active . '">';
64 if (is_array($selectIcon)) {
65 $html[] = '<a href="#" title="' . htmlspecialchars($selectIcon['title'], ENT_COMPAT, 'UTF-8', false) . '" data-select-index="' . htmlspecialchars((string)$selectIcon['index']) . '">';
66 $html[] = $selectIcon['icon'];
67 $html[] = '</a>';
68 }
69 $html[] = '</div>';
70 }
71 $html[] = '</div>';
72 $html[] = '</div>';
73 }
74
75 $result['html'] = implode(LF, $html);
76 return $result;
77 }
78 }