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

FluidTYPO3 / flux / 12237686280

09 Dec 2024 02:27PM UTC coverage: 92.9% (-0.5%) from 93.421%
12237686280

push

github

NamelessCoder
[TER] 10.1.0

7013 of 7549 relevant lines covered (92.9%)

56.22 hits per line

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

96.84
/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

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

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

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

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

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

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

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

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

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

126
    protected function prepareConfiguration(string $type): array
127
    {
128
        return [
288✔
129
            'type' => $type,
288✔
130
            'transform' => $this->getTransform(),
288✔
131
            'default' => $this->getDefault(),
288✔
132
        ];
288✔
133
    }
134

135
    public function isNative(): bool
136
    {
137
        return $this->native;
42✔
138
    }
139

140
    public function setNative(bool $native): self
141
    {
142
        $this->native = $native;
258✔
143
        return $this;
258✔
144
    }
145

146
    public function setRequired(bool $required): self
147
    {
148
        $this->required = $required;
288✔
149
        return $this;
288✔
150
    }
151

152
    public function getRequired(): bool
153
    {
154
        return $this->required;
120✔
155
    }
156

157
    /**
158
     * @param mixed $default
159
     */
160
    public function setDefault($default): self
161
    {
162
        $this->default = $default;
534✔
163
        return $this;
534✔
164
    }
165

166
    /**
167
     * @return mixed
168
     */
169
    public function getDefault()
170
    {
171
        return $this->default;
330✔
172
    }
173

174
    /**
175
     * @param string|array|null $displayCondition
176
     */
177
    public function setDisplayCondition($displayCondition): self
178
    {
179
        $this->displayCondition = $displayCondition;
282✔
180
        return $this;
282✔
181
    }
182

183
    /**
184
     * @return string|array|null
185
     */
186
    public function getDisplayCondition()
187
    {
188
        return $this->displayCondition;
288✔
189
    }
190

191
    public function setRequestUpdate(bool $requestUpdate): self
192
    {
193
        $this->requestUpdate = $requestUpdate;
546✔
194
        return $this;
546✔
195
    }
196

197
    public function getRequestUpdate(): bool
198
    {
199
        return $this->requestUpdate;
312✔
200
    }
201

202
    public function setExclude(bool $exclude): self
203
    {
204
        $this->exclude = $exclude;
264✔
205
        return $this;
264✔
206
    }
207

208
    public function getExclude(): bool
209
    {
210
        return $this->exclude;
288✔
211
    }
212

213
    public function setValidate(?string $validate): self
214
    {
215
        $this->validate = $validate;
366✔
216
        return $this;
366✔
217
    }
218

219
    public function getConfig(): array
220
    {
221
        return $this->config;
288✔
222
    }
223

224
    public function setConfig(array $config): self
225
    {
226
        $this->config = $config;
276✔
227
        return $this;
276✔
228
    }
229

230
    public function getValidate(): ?string
231
    {
232
        if (!$this->getRequired()) {
120✔
233
            $validate = $this->validate;
96✔
234
        } else {
235
            if (empty($this->validate)) {
24✔
236
                $validate = 'required';
12✔
237
            } else {
238
                $validators = GeneralUtility::trimExplode(',', $this->validate);
12✔
239
                array_push($validators, 'required');
12✔
240
                $validate = implode(',', $validators);
12✔
241
            }
242
        }
243
        return $validate;
120✔
244
    }
245

246
    public function getPosition(): ?string
247
    {
248
        return $this->position;
6✔
249
    }
250

251
    public function setPosition(?string $position): self
252
    {
253
        $this->position = $position;
258✔
254
        return $this;
258✔
255
    }
256

257
    public function setClearable(bool $clearable): self
258
    {
259
        $this->clearable = (boolean) $clearable;
498✔
260
        return $this;
498✔
261
    }
262

263
    public function getClearable(): bool
264
    {
265
        return $this->clearable;
414✔
266
    }
267

268
    public function getProtectable(): bool
269
    {
270
        return $this->protectable;
282✔
271
    }
272

273
    public function setProtectable(bool $protectable): self
274
    {
275
        $this->protectable = $protectable;
264✔
276
        return $this;
264✔
277
    }
278
}
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