• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / flux / 14774311267

01 May 2025 11:00AM UTC coverage: 93.246% (+0.3%) from 92.9%
14774311267

push

github

NamelessCoder
[TER] 11.0.0

7083 of 7596 relevant lines covered (93.25%)

66.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.03
/Classes/Form/AbstractFormField.php
1
<?php
2
declare(strict_types=1);
3
namespace FluidTYPO3\Flux\Form;
4

5
/*
6
 * This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.md file that was distributed with this source code.
10
 */
11

12
use FluidTYPO3\Flux\Form\Container\Section;
13
use FluidTYPO3\Flux\UserFunction\ClearValueWizard;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
16

17
abstract class AbstractFormField extends AbstractFormComponent implements FieldInterface
18
{
19
    /**
20
     * @var mixed
21
     */
22
    protected $default;
23

24
    /**
25
     * Display condition - see https://docs.typo3.org/typo3cms/TCAReference/Reference/Columns/Index.html#displaycond
26
     *
27
     * @var array|string|null
28
     */
29
    protected $displayCondition = null;
30

31
    protected bool $native = false;
32
    protected bool $required = false;
33
    protected bool $requestUpdate = false;
34
    protected bool $inherit = true;
35
    protected bool $inheritEmpty = false;
36
    protected bool $clearable = false;
37
    protected bool $protectable = false;
38
    protected bool $exclude = false;
39
    protected ?string $validate = null;
40
    protected ?string $position = null;
41
    protected array $config = [];
42

43
    public static function create(array $settings = []): FormInterface
44
    {
45
        if (!isset($settings['type'])) {
567✔
46
            $settings['type'] = static::class;
7✔
47
        }
48
        if ('Section' === $settings['type']) {
567✔
49
            return Section::create($settings);
133✔
50
        } else {
51
            $prefix = AbstractFormComponent::NAMESPACE_FIELD . '\\';
434✔
52
            $type = $settings['type'];
434✔
53
            $className = str_replace('/', '\\', $type);
434✔
54
            $className = class_exists($prefix . $className) ? $prefix . $className : $className;
434✔
55
        }
56
        if (!class_exists($className)) {
434✔
57
            $className = $settings['type'];
133✔
58
        }
59
        if (!class_exists($className)) {
434✔
60
            throw new \RuntimeException(
133✔
61
                sprintf(
133✔
62
                    'Invalid class- or type-name used in type of field "%s"; "%s" is invalid',
133✔
63
                    $settings['name'] ?? '(unknown)',
133✔
64
                    $className
133✔
65
                ),
133✔
66
                1375373527
133✔
67
            );
133✔
68
        }
69
        /** @var FieldInterface $object */
70
        $object = GeneralUtility::makeInstance($className);
301✔
71
        foreach ($settings as $settingName => $settingValue) {
301✔
72
            $setterMethodName = 'set' . ucfirst($settingName);
301✔
73
            if (true === method_exists($object, $setterMethodName)) {
301✔
74
                $object->{$setterMethodName}($settingValue);
280✔
75
            }
76
        }
77
        return $object;
301✔
78
    }
79

80
    /**
81
     * Creates a TCEforms configuration array based on the
82
     * configuration stored in this ViewHelper. Calls the
83
     * expected-to-be-overridden stub method getConfiguration()
84
     * to return the TCE field configuration - see that method
85
     * for information about how to implement that method.
86
     */
87
    public function build(): array
88
    {
89
        if (!$this->getEnabled()) {
474✔
90
            return [];
133✔
91
        }
92

93
        // The "config" section consists of whichever configuration arry the component built, but with
94
        // priority to any options set directly as raw TCA field config options in $this->config.
95
        $configuration = array_replace($this->buildConfiguration(), $this->getConfig());
341✔
96
        $filterClosure = function ($value) {
341✔
97
            return $value !== null && $value !== '' && $value !== [];
334✔
98
        };
341✔
99
        $configuration = array_filter($configuration, $filterClosure);
341✔
100
        $fieldStructureArray = [
341✔
101
            'label' => $this->getLabel(),
341✔
102
            'exclude' => intval($this->getExclude()),
341✔
103
            'config' => $configuration
341✔
104
        ];
341✔
105
        if (($displayCondition = $this->getDisplayCondition())) {
341✔
106
            $fieldStructureArray['displayCond'] = $displayCondition;
7✔
107
        }
108

109
        if ($this->getClearable()) {
341✔
110
            $fieldStructureArray['config']['fieldWizard']['fluxClearValue'] = [
133✔
111
                'renderType' => 'fluxClearValue',
133✔
112
            ];
133✔
113
        }
114

115
        if ($this->getProtectable() && $this->getInherit()) {
341✔
116
            $fieldStructureArray['config']['fieldWizard']['fluxProtectValue'] = [
×
117
                'renderType' => 'fluxProtectValue',
×
118
            ];
×
119
        }
120

121
        if ($this->getRequestUpdate()) {
341✔
122
            $fieldStructureArray['onChange'] = 'reload';
47✔
123
        }
124
        return $fieldStructureArray;
341✔
125
    }
126

127
    protected function prepareConfiguration(string $type): array
128
    {
129
        $config = [
348✔
130
            'type' => $type,
348✔
131
            'transform' => $this->getTransform(),
348✔
132
            'default' => $this->getDefault(),
348✔
133
        ];
348✔
134
        if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '12.0', '>=')
348✔
135
            && $this->getRequired()
348✔
136
        ) {
137
            $config['required'] = $this->getRequired();
12✔
138
        }
139
        return $config;
348✔
140
    }
141

142
    public function isNative(): bool
143
    {
144
        return $this->native;
49✔
145
    }
146

147
    public function setNative(bool $native): self
148
    {
149
        $this->native = $native;
301✔
150
        return $this;
301✔
151
    }
152

153
    public function setRequired(bool $required): self
154
    {
155
        $this->required = $required;
336✔
156
        return $this;
336✔
157
    }
158

159
    public function getRequired(): bool
160
    {
161
        return $this->required;
236✔
162
    }
163

164
    /**
165
     * @param mixed $default
166
     */
167
    public function setDefault($default): self
168
    {
169
        $this->default = $default;
623✔
170
        return $this;
623✔
171
    }
172

173
    /**
174
     * @return mixed
175
     */
176
    public function getDefault()
177
    {
178
        return $this->default;
397✔
179
    }
180

181
    /**
182
     * @param string|array|null $displayCondition
183
     */
184
    public function setDisplayCondition($displayCondition): self
185
    {
186
        $this->displayCondition = $displayCondition;
329✔
187
        return $this;
329✔
188
    }
189

190
    /**
191
     * @return string|array|null
192
     */
193
    public function getDisplayCondition()
194
    {
195
        return $this->displayCondition;
348✔
196
    }
197

198
    public function setRequestUpdate(bool $requestUpdate): self
199
    {
200
        $this->requestUpdate = $requestUpdate;
637✔
201
        return $this;
637✔
202
    }
203

204
    public function getRequestUpdate(): bool
205
    {
206
        return $this->requestUpdate;
376✔
207
    }
208

209
    public function setExclude(bool $exclude): self
210
    {
211
        $this->exclude = $exclude;
308✔
212
        return $this;
308✔
213
    }
214

215
    public function getExclude(): bool
216
    {
217
        return $this->exclude;
348✔
218
    }
219

220
    public function setValidate(?string $validate): self
221
    {
222
        $this->validate = $validate;
427✔
223
        return $this;
427✔
224
    }
225

226
    public function getConfig(): array
227
    {
228
        return $this->config;
348✔
229
    }
230

231
    public function setConfig(array $config): self
232
    {
233
        $this->config = $config;
322✔
234
        return $this;
322✔
235
    }
236

237
    public function getValidate(): ?string
238
    {
239
        if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '12.0', '>=')) {
140✔
240
            return $this->validate;
60✔
241
        }
242

243
        if (!$this->getRequired()) {
80✔
244
            $validate = $this->validate;
64✔
245
        } else {
246
            if (empty($this->validate)) {
16✔
247
                $validate = 'required';
8✔
248
            } else {
249
                $validators = GeneralUtility::trimExplode(',', $this->validate);
8✔
250
                array_push($validators, 'required');
8✔
251
                $validate = implode(',', $validators);
8✔
252
            }
253
        }
254
        return $validate;
80✔
255
    }
256

257
    public function getPosition(): ?string
258
    {
259
        return $this->position;
7✔
260
    }
261

262
    public function setPosition(?string $position): self
263
    {
264
        $this->position = $position;
301✔
265
        return $this;
301✔
266
    }
267

268
    public function setClearable(bool $clearable): self
269
    {
270
        $this->clearable = (boolean) $clearable;
581✔
271
        return $this;
581✔
272
    }
273

274
    public function getClearable(): bool
275
    {
276
        return $this->clearable;
495✔
277
    }
278

279
    public function getProtectable(): bool
280
    {
281
        return $this->protectable;
341✔
282
    }
283

284
    public function setProtectable(bool $protectable): self
285
    {
286
        $this->protectable = $protectable;
308✔
287
        return $this;
308✔
288
    }
289
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc