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

FluidTYPO3 / flux / 18588861398

17 Oct 2025 09:40AM UTC coverage: 92.656% (-0.1%) from 92.767%
18588861398

push

github

NamelessCoder
[TER] 11.1.0

7090 of 7652 relevant lines covered (92.66%)

66.89 hits per line

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

97.09
/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'])) {
574✔
46
            $settings['type'] = static::class;
14✔
47
        }
48
        if ('Section' === $settings['type']) {
574✔
49
            return Section::create($settings);
133✔
50
        } else {
51
            $prefix = AbstractFormComponent::NAMESPACE_FIELD . '\\';
441✔
52
            $type = $settings['type'];
441✔
53
            $className = str_replace('/', '\\', $type);
441✔
54
            $className = class_exists($prefix . $className) ? $prefix . $className : $className;
441✔
55
        }
56
        if (!class_exists($className)) {
441✔
57
            $className = $settings['type'];
133✔
58
        }
59
        if (!class_exists($className)) {
441✔
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);
308✔
71
        foreach ($settings as $settingName => $settingValue) {
308✔
72
            $setterMethodName = 'set' . ucfirst($settingName);
308✔
73
            if (true === method_exists($object, $setterMethodName)) {
308✔
74
                $object->{$setterMethodName}($settingValue);
287✔
75
            }
76
        }
77
        return $object;
308✔
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()) {
481✔
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());
348✔
96
        $filterClosure = function ($value) {
348✔
97
            return $value !== null && $value !== '' && $value !== [];
341✔
98
        };
348✔
99
        $configuration = array_filter($configuration, $filterClosure);
348✔
100
        $fieldStructureArray = [
348✔
101
            'label' => $this->getLabel(),
348✔
102
            'exclude' => intval($this->getExclude()),
348✔
103
            'config' => $configuration
348✔
104
        ];
348✔
105
        if (($description = $this->getDescription()) && $description !== null) {
348✔
106
            $fieldStructureArray['description'] = $this->getDescription();
7✔
107
        }
108
        if (($displayCondition = $this->getDisplayCondition())) {
348✔
109
            $fieldStructureArray['displayCond'] = $displayCondition;
7✔
110
        }
111

112
        if ($this->getClearable()) {
348✔
113
            $fieldStructureArray['config']['fieldWizard']['fluxClearValue'] = [
133✔
114
                'renderType' => 'fluxClearValue',
133✔
115
            ];
133✔
116
        }
117

118
        if ($this->getProtectable() && $this->getInherit()) {
348✔
119
            $fieldStructureArray['config']['fieldWizard']['fluxProtectValue'] = [
×
120
                'renderType' => 'fluxProtectValue',
×
121
            ];
×
122
        }
123

124
        if ($this->getRequestUpdate()) {
348✔
125
            $fieldStructureArray['onChange'] = 'reload';
47✔
126
        }
127
        return $fieldStructureArray;
348✔
128
    }
129

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

145
    public function isNative(): bool
146
    {
147
        return $this->native;
49✔
148
    }
149

150
    public function setNative(bool $native): self
151
    {
152
        $this->native = $native;
315✔
153
        return $this;
315✔
154
    }
155

156
    public function setRequired(bool $required): self
157
    {
158
        $this->required = $required;
350✔
159
        return $this;
350✔
160
    }
161

162
    public function getRequired(): bool
163
    {
164
        return $this->required;
243✔
165
    }
166

167
    /**
168
     * @param mixed $default
169
     */
170
    public function setDefault($default): self
171
    {
172
        $this->default = $default;
637✔
173
        return $this;
637✔
174
    }
175

176
    /**
177
     * @return mixed
178
     */
179
    public function getDefault()
180
    {
181
        return $this->default;
411✔
182
    }
183

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

193
    /**
194
     * @return string|array|null
195
     */
196
    public function getDisplayCondition()
197
    {
198
        return $this->displayCondition;
362✔
199
    }
200

201
    public function setRequestUpdate(bool $requestUpdate): self
202
    {
203
        $this->requestUpdate = $requestUpdate;
651✔
204
        return $this;
651✔
205
    }
206

207
    public function getRequestUpdate(): bool
208
    {
209
        return $this->requestUpdate;
383✔
210
    }
211

212
    public function setExclude(bool $exclude): self
213
    {
214
        $this->exclude = $exclude;
329✔
215
        return $this;
329✔
216
    }
217

218
    public function getExclude(): bool
219
    {
220
        return $this->exclude;
362✔
221
    }
222

223
    public function setValidate(?string $validate): self
224
    {
225
        $this->validate = $validate;
441✔
226
        return $this;
441✔
227
    }
228

229
    public function getConfig(): array
230
    {
231
        return $this->config;
362✔
232
    }
233

234
    public function setConfig(array $config): self
235
    {
236
        $this->config = $config;
350✔
237
        return $this;
350✔
238
    }
239

240
    public function getValidate(): ?string
241
    {
242
        if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '12.0', '>=')) {
147✔
243
            return $this->validate;
63✔
244
        }
245

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

260
    public function getPosition(): ?string
261
    {
262
        return $this->position;
7✔
263
    }
264

265
    public function setPosition(?string $position): self
266
    {
267
        $this->position = $position;
315✔
268
        return $this;
315✔
269
    }
270

271
    public function setClearable(bool $clearable): self
272
    {
273
        $this->clearable = (boolean) $clearable;
602✔
274
        return $this;
602✔
275
    }
276

277
    public function getClearable(): bool
278
    {
279
        return $this->clearable;
509✔
280
    }
281

282
    public function getProtectable(): bool
283
    {
284
        return $this->protectable;
348✔
285
    }
286

287
    public function setProtectable(bool $protectable): self
288
    {
289
        $this->protectable = $protectable;
329✔
290
        return $this;
329✔
291
    }
292
}
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