AbstractFormSelectElement::getLanguageService()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Aoe\FeatureFlag\Form\Element;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2024 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use Aoe\FeatureFlag\Domain\Repository\FeatureFlagRepository;
30
use Aoe\FeatureFlag\Domain\Repository\MappingRepository;
31
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
32
use TYPO3\CMS\Backend\Form\NodeFactory;
33
use TYPO3\CMS\Core\Localization\LanguageService;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Core\Utility\MathUtility;
36
use TYPO3\CMS\Core\Utility\StringUtility;
37
38
abstract class AbstractFormSelectElement extends AbstractFormElement
39
{
40
    protected object $featureFlagRepository;
41
42
    protected object $mappingRepository;
43
44
    /**
45
     * Container objects give $nodeFactory down to other containers.
46
     */
47
    public function __construct(
48
        NodeFactory $nodeFactory,
49
        array $data,
50 2
        ?FeatureFlagRepository $featureFlagRepository = null,
51
        ?MappingRepository $mappingRepository = null
52
    ) {
53
        parent::__construct($nodeFactory, $data);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Backend\Form\E...mElement::__construct() has been deprecated: since TYPO3 v12.4. Default constructor will be removed in v13. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

53
        /** @scrutinizer ignore-deprecated */ parent::__construct($nodeFactory, $data);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
54
55
        $this->featureFlagRepository = $featureFlagRepository ??
56 2
            GeneralUtility::makeInstance(FeatureFlagRepository::class);
57
58 2
        $this->mappingRepository = $mappingRepository ??
59
            GeneralUtility::makeInstance(MappingRepository::class);
60
    }
61 2
62
    /**
63 2
     * This will render a selector box element, or possibly a special construction with two selector boxes.
64
     */
65
    public function renderElement(array $optionElements): array
66
    {
67
        $resultArray = $this->initializeResultArray();
68 2
        $parameterArray = $this->data['parameterArray'];
69
70 2
        // Field configuration from TCA:
71 2
        $config = $parameterArray['fieldConf']['config'];
72
73
        $selectElement = $this->renderSelectElement($optionElements, $parameterArray, $config);
74 2
75
        $width = MathUtility::forceIntegerInRange(
76 2
            $config['width'] ?? $this->defaultInputWidth,
77
            $this->minimumInputWidth,
78 2
            $this->maxInputWidth
79 2
        );
80 2
        $maxWidth = $this->formMaxWidth($width);
81 2
82
        $html = [];
83 2
        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
84
        $html[] = '<div class="form-control-wrap" style="max-width: ' . $maxWidth . 'px">';
85 2
        $html[] = '<div class="form-wizards-wrap">';
86 2
        $html[] = '<div class="form-wizards-element">';
87 2
        $html[] = $selectElement;
88 2
        $html[] = '</div>';
89 2
        $html[] = '</div>';
90 2
        $html[] = '</div>';
91 2
92 2
        $resultArray['html'] = implode(LF, $html);
93 2
94
        return $resultArray;
95 2
    }
96
97 2
    /**
98
     * Renders a <select> element
99
     */
100
    protected function renderSelectElement(
101
        array $options,
102
        array $parameterArray,
103 2
        array $config
104
    ): string {
105
        $attributes = [
106
            'id' => StringUtility::getUniqueId('tceforms-select-'),
107
            'name' => $parameterArray['itemFormElName'],
108
            'class' => 'form-select tceforms-select',
109 2
            'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
110 2
            'disabled' => (string) !empty($config['readOnly']),
111 2
            'size' => $config['size'],
112 2
        ];
113 2
114 2
        $optionElements = [];
115
        foreach ($options as $option) {
116
            $optionAttributes = [];
117 2
            if ($option['isSelected']) {
118 2
                $optionAttributes['selected'] = 'selected';
119 2
            }
120 2
121 2
            $optionElements[] = $this->renderOptionElement($option['value'], $option['name'], $optionAttributes);
122
        }
123 2
124
        $html = [];
125
        $html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
126 2
        $html[] = implode(LF, $optionElements);
127 2
        $html[] = '</select>';
128 2
129 2
        return implode(LF, $html);
130
    }
131 2
132
    /**
133
     * Renders a single <option> element
134
     */
135
    protected function renderOptionElement(string $value, string $label, array $attributes = []): string
136
    {
137 2
        $attributes['value'] = $value;
138
        $html = [
139 2
            '<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>',
140
            htmlspecialchars($label, ENT_COMPAT, 'UTF-8', false),
141 2
            '</option>',
142 2
143 2
        ];
144
145
        return implode('', $html);
146
    }
147 2
148
    protected function getLanguageService(): LanguageService
149
    {
150 1
        return $GLOBALS['LANG'];
151
    }
152
}
153