Passed
Push — refactoring/typo3v10 ( 064584...244caa )
by Felix
07:56
created

AbstractFormSelectElement::getLanguageService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace Aoe\FeatureFlag\Form\Element;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2021 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\FeatureFlag\Domain\Repository\FeatureFlagRepository;
29
use Aoe\FeatureFlag\Domain\Repository\MappingRepository;
30
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
31
use TYPO3\CMS\Backend\Form\NodeFactory;
32
use TYPO3\CMS\Core\Localization\LanguageService;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Core\Utility\MathUtility;
35
use TYPO3\CMS\Core\Utility\StringUtility;
36
use TYPO3\CMS\Extbase\Object\ObjectManager;
37
38
abstract class AbstractFormSelectElement extends AbstractFormElement
39
{
40
    /**
41
     * @var FeatureFlagRepository
42
     */
43
    protected $featureFlagRepository;
44
45
    /**
46
     * @var MappingRepository
47
     */
48
    protected $mappingRepository;
49
50
    /**
51
     * Container objects give $nodeFactory down to other containers.
52
     *
53
     * @param NodeFactory $nodeFactory
54
     * @param array $data
55
     * @param FeatureFlagRepository|null $featureFlagRepository
56
     * @param MappingRepository|null $mappingRepository
57
     */
58 2
    public function __construct(
59
        NodeFactory $nodeFactory,
60
        array $data,
61
        FeatureFlagRepository $featureFlagRepository = null,
62
        MappingRepository $mappingRepository = null
63
    ) {
64 2
        parent::__construct($nodeFactory, $data);
65
66 2
        $this->featureFlagRepository = $featureFlagRepository ??
67
            GeneralUtility::makeInstance(ObjectManager::class)->get(FeatureFlagRepository::class);
68
69 2
        $this->mappingRepository = $mappingRepository ??
70
            GeneralUtility::makeInstance(ObjectManager::class)->get(MappingRepository::class);
71 2
    }
72
73
    /**
74
     * This will render a selector box element, or possibly a special construction with two selector boxes.
75
     *
76
     * @param array $optionElements
77
     * @return array As defined in initializeResultArray() of AbstractNode
78
     */
79 2
    public function renderElement(array $optionElements)
80
    {
81 2
        $resultArray = $this->initializeResultArray();
82 2
        $parameterArray = $this->data['parameterArray'];
83
84
        // Field configuration from TCA:
85 2
        $config = $parameterArray['fieldConf']['config'];
86
87 2
        $selectElement = $this->renderSelectElement($optionElements, $parameterArray, $config);
88
89 2
        $width = MathUtility::forceIntegerInRange(
90 2
            $config['width'] ?: $this->defaultInputWidth,
91 2
            $this->minimumInputWidth,
92 2
            $this->maxInputWidth
93
        );
94 2
        $maxWidth = $this->formMaxWidth($width);
95
96 2
        $html = [];
97 2
        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
98 2
        $html[] =   '<div class="form-control-wrap" style="max-width: ' . $maxWidth . 'px">';
99 2
        $html[] =       '<div class="form-wizards-wrap">';
100 2
        $html[] =           '<div class="form-wizards-element">';
101 2
        $html[] =               $selectElement;
102 2
        $html[] =           '</div>';
103 2
        $html[] =   '</div>';
104 2
        $html[] = '</div>';
105
106 2
        $resultArray['html'] = implode(LF, $html);
107
108 2
        return $resultArray;
109
    }
110
111
    /**
112
     * Renders a <select> element
113
     *
114
     * @param array $options
115
     * @param array $parameterArray
116
     * @param array $config Field configuration
117
     * @return string
118
     */
119 2
    protected function renderSelectElement(
120
        array $options,
121
        array $parameterArray,
122
        array $config
123
    ) {
124
        $attributes = [
125 2
            'id' => StringUtility::getUniqueId('tceforms-select-'),
126 2
            'name' => $parameterArray['itemFormElName'],
127 2
            'class' => 'form-control tceforms-select',
128 2
            'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
129 2
            'disabled' => !empty($config['readOnly']),
130 2
            'size' => (int)$config['size']
131
        ];
132
133 2
        $optionElements = [];
134 2
        foreach ($options as $option) {
135 2
            $optionAttributes = [];
136 2
            if ($option['isSelected']) {
137 2
                $optionAttributes['selected'] = 'selected';
138
            }
139 2
            $optionElements[] = $this->renderOptionElement($option['value'], $option['name'], $optionAttributes);
140
        }
141
142 2
        $html = [];
143 2
        $html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
144 2
        $html[] =   implode(LF, $optionElements);
145 2
        $html[] = '</select>';
146
147 2
        return implode(LF, $html);
148
    }
149
150
    /**
151
     * Renders a single <option> element
152
     *
153
     * @param string $value The option value
154
     * @param string $label The option label
155
     * @param array $attributes Map of attribute names and values
156
     * @return string
157
     */
158 2
    protected function renderOptionElement($value, $label, array $attributes = [])
159
    {
160 2
        $attributes['value'] = $value;
161
        $html = [
162 2
            '<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>',
163 2
            htmlspecialchars($label, ENT_COMPAT, 'UTF-8', false),
164 2
            '</option>'
165
166
        ];
167
168 2
        return implode('', $html);
169
    }
170
171
    /**
172
     * @return LanguageService
173
     */
174 1
    protected function getLanguageService()
175
    {
176 1
        return $GLOBALS['LANG'];
177
    }
178
}
179