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

CPS-IT / handlebars-forms / 23530010973

25 Mar 2026 07:34AM UTC coverage: 0.787% (-0.008%) from 0.795%
23530010973

push

github

eliashaeussler
[TASK] Pass additional attributes to `formvh:form.password` view helper

0 of 1 new or added line in 1 file covered. (0.0%)

95 existing lines in 6 files now uncovered.

7 of 889 relevant lines covered (0.79%)

0.03 hits per line

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

0.0
/Classes/ContentObject/ValidationResultsContentObject.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars_forms".
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17

18
namespace CPSIT\Typo3HandlebarsForms\ContentObject;
19

20
use CPSIT\Typo3HandlebarsForms\Fluid;
21
use Psr\Http\Message;
22
use Symfony\Component\DependencyInjection;
23
use TYPO3\CMS\Extbase;
24
use TYPO3\CMS\Form;
25

26
/**
27
 * ValidationResultsContentObject
28
 *
29
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
30
 * @license GPL-2.0-or-later
31
 */
32
#[DependencyInjection\Attribute\AutoconfigureTag('frontend.contentobject', ['identifier' => 'HBS_VALIDATION_RESULTS'])]
33
final class ValidationResultsContentObject extends AbstractHandlebarsFormsContentObject
34
{
35
    public function __construct(
×
36
        private readonly Fluid\ViewHelperInvoker $viewHelperInvoker,
37
    ) {}
×
38

39
    protected function resolve(array $configuration, Context\ValueResolutionContext $context): mixed
×
40
    {
41
        $outputInstruction = $configuration['output'] ?? null;
×
42
        $outputConfiguration = $configuration['output.'] ?? null;
×
43
        $renderable = $context->renderable;
×
44

45
        // Resolve property path from renderable
46
        if ($renderable instanceof Form\Domain\Model\Renderable\AbstractRenderable) {
×
47
            $property = $renderable->getRootForm()->getIdentifier() . '.' . $renderable->getIdentifier();
×
48
        } else {
49
            $property = $renderable->getIdentifier();
×
50
        }
51

52
        $request = $context->viewModel->renderingContext->getAttribute(Message\ServerRequestInterface::class);
×
53
        $extbaseRequestParameters = $request->getAttribute('extbase');
×
54

55
        // Early return when content object was requested outside of extbase context
56
        if (!($extbaseRequestParameters instanceof Extbase\Mvc\ExtbaseRequestParameters)) {
×
57
            return null;
×
58
        }
59

60
        $validationResults = $extbaseRequestParameters->getOriginalRequestMappingResults()->forProperty($property);
×
61

62
        // Normalize output configuration
63
        if (!is_array($outputConfiguration)) {
×
64
            $outputConfiguration = null;
×
65
        }
66

67
        // Resolve validation results by a single rendering instruction
68
        if (is_string($outputInstruction)) {
×
69
            return $this->processRenderingInstruction(
×
70
                $context,
×
71
                $validationResults,
×
72
                $outputInstruction,
×
73
                $outputConfiguration ?? [],
×
74
            );
×
75
        }
76

77
        // Resolve complex rendering configuration
78
        if (is_array($outputConfiguration)) {
×
79
            return $this->processRenderingConfiguration($context, $validationResults, $outputConfiguration);
×
80
        }
81

82
        // Return raw results in case no rendering instructions are specified
83
        return $validationResults;
×
84
    }
85

86
    /**
87
     * @param array<string|int, mixed> $configuration
88
     * @return array<string|int, mixed>
89
     */
90
    private function processRenderingConfiguration(
×
91
        Context\ValueResolutionContext $context,
92
        Extbase\Error\Result $result,
93
        array $configuration,
94
        bool $retainUnmatchedRenderingConfiguration = false,
95
    ): array {
96
        $processedData = [];
×
97

98
        foreach ($configuration as $key => $value) {
×
99
            $keyWithoutDot = rtrim((string)$key, '.');
×
100
            $keyWithDot = $keyWithoutDot . '.';
×
101

102
            if (is_array($value)) {
×
103
                if (!array_key_exists($keyWithoutDot, $processedData)) {
×
104
                    // Process nested rendering configuration
105
                    $processedData[$keyWithoutDot] = $this->processRenderingConfiguration(
×
106
                        $context,
×
107
                        $result,
×
108
                        $value,
×
109
                        $retainUnmatchedRenderingConfiguration,
×
110
                    );
×
111
                } elseif ($retainUnmatchedRenderingConfiguration) {
×
112
                    // Keep non-resolvable configuration (may be resolved differently)
113
                    $processedData[$keyWithDot] = $value;
×
114
                }
115
            } elseif (is_string($value)) {
×
116
                $valueConfiguration = $configuration[$keyWithDot] ?? [];
×
117

118
                if (!is_array($valueConfiguration)) {
×
119
                    $valueConfiguration = [];
×
120
                }
121

UNCOV
122
                $processedData[$keyWithoutDot] = $this->processRenderingInstruction(
×
UNCOV
123
                    $context,
×
UNCOV
124
                    $result,
×
125
                    $value,
×
UNCOV
126
                    $valueConfiguration,
×
UNCOV
127
                );
×
128
            }
129
        }
130

131
        return $processedData;
×
132
    }
133

134
    /**
135
     * @param array<string, mixed> $configuration
136
     */
137
    private function processRenderingInstruction(
×
138
        Context\ValueResolutionContext $context,
139
        Extbase\Error\Result $result,
140
        string $value,
141
        array $configuration,
142
    ): mixed {
143
        return match ($value) {
×
144
            'EACH_ERROR' => $this->processErrors($context, $result, $configuration),
×
UNCOV
145
            'EACH_RENDERABLE' => $this->processRenderables($context, $result, $configuration),
×
UNCOV
146
            'ERROR_MESSAGE' => $this->processErrorMessage($context, $result),
×
UNCOV
147
            'PROPERTY' => $this->processProperty($context, $configuration),
×
UNCOV
148
            'RESULT' => $this->processResult($result, $configuration),
×
UNCOV
149
            default => $value,
×
UNCOV
150
        };
×
151
    }
152

153
    /**
154
     * @param array<string|int, mixed> $configuration
155
     * @return array<string, list<array<string|int, mixed>>>|list<array<string|int, mixed>>
156
     */
157
    private function processErrors(
×
158
        Context\ValueResolutionContext $context,
159
        Extbase\Error\Result $result,
160
        array $configuration,
161
    ): array {
162
        $renderable = $context->renderable;
×
163
        $processedErrors = [];
×
164

UNCOV
165
        foreach ($result->getFlattenedErrors() as $propertyPath => $errors) {
×
UNCOV
166
            $currentRenderable = $renderable;
×
167

168
            if ($propertyPath !== '' && $renderable instanceof Form\Domain\Runtime\FormRuntime) {
×
UNCOV
169
                $currentRenderable = $renderable->getFormDefinition()->getElementByIdentifier($propertyPath);
×
170
            }
171

172
            // Skip errors if current renderable could not be resolved
173
            if ($currentRenderable === null) {
×
174
                continue;
×
175
            }
176

177
            $processedResult = [];
×
178

179
            foreach ($errors as $error) {
×
180
                $errorResult = new Extbase\Error\Result();
×
181
                $errorResult->addError($error);
×
182

UNCOV
183
                $processedResult[] = $this->processRenderingConfiguration(
×
184
                    $context->withRenderable($currentRenderable),
×
UNCOV
185
                    $errorResult,
×
UNCOV
186
                    $configuration,
×
187
                );
×
188
            }
189

190
            $processedErrors[$propertyPath] = $processedResult;
×
191
        }
192

193
        if ($renderable instanceof Form\Domain\Model\Renderable\CompositeRenderableInterface
×
UNCOV
194
            || $renderable instanceof Form\Domain\Runtime\FormRuntime
×
195
        ) {
196
            return $processedErrors;
×
197
        }
198

199
        $firstProcessedError = reset($processedErrors);
×
200

UNCOV
201
        if ($firstProcessedError !== false) {
×
UNCOV
202
            return $firstProcessedError;
×
203
        }
204

UNCOV
205
        return [];
×
206
    }
207

208
    /**
209
     * @param array<string|int, mixed> $configuration
210
     * @return array<string, mixed>
211
     */
212
    private function processRenderables(
×
213
        Context\ValueResolutionContext $context,
214
        Extbase\Error\Result $result,
215
        array $configuration,
216
    ): array {
217
        $renderable = $context->renderable;
×
218
        $processedRenderables = [];
×
219

UNCOV
220
        foreach ($result->getFlattenedErrors() as $propertyPath => $errors) {
×
UNCOV
221
            $currentRenderable = $renderable;
×
222

223
            if ($propertyPath !== '' && $renderable instanceof Form\Domain\Runtime\FormRuntime) {
×
UNCOV
224
                $currentRenderable = $renderable->getFormDefinition()->getElementByIdentifier($propertyPath);
×
225
            }
226

227
            // Skip errors if current renderable could not be resolved
228
            if ($currentRenderable === null) {
×
229
                continue;
×
230
            }
231

232
            // Process validation result first
UNCOV
233
            $processedRenderable = $this->processRenderingConfiguration(
×
UNCOV
234
                $context->withRenderable($currentRenderable),
×
235
                $result->forProperty($propertyPath),
×
UNCOV
236
                $configuration,
×
UNCOV
237
                true,
×
238
            );
×
239

240
            // Normalize rendering configuration for post-processing
241
            $processedRenderable = $this->normalizeRenderingConfiguration($processedRenderable);
×
242

243
            // Post-process renderable
UNCOV
244
            $processedRenderables[$propertyPath] = $context->process($processedRenderable, $currentRenderable);
×
245
        }
246

UNCOV
247
        return $processedRenderables;
×
248
    }
249

250
    /**
251
     * @param array<string|int, mixed> $processedRenderable
252
     * @return array<string|int, mixed>
253
     */
254
    private function normalizeRenderingConfiguration(array $processedRenderable): array
×
255
    {
UNCOV
256
        foreach ($processedRenderable as $key => $value) {
×
UNCOV
257
            $keyWithoutDot = rtrim((string)$key, '.');
×
258
            $keyWithDot = $keyWithoutDot . '.';
×
259

UNCOV
260
            if (!is_array($value) || $key === $keyWithDot) {
×
UNCOV
261
                continue;
×
262
            }
263

264
            if (!array_key_exists($keyWithDot, $processedRenderable)) {
×
UNCOV
265
                $processedRenderable[$keyWithDot] = $this->normalizeRenderingConfiguration($value);
×
266
            }
267

UNCOV
268
            if (!is_array($processedRenderable[$keyWithDot])) {
×
UNCOV
269
                $processedRenderable[$keyWithDot] = [];
×
270
            }
271

272
            $processedRenderable[$keyWithDot] = array_replace_recursive($processedRenderable[$keyWithDot], $value);
×
273

UNCOV
274
            unset($processedRenderable[$keyWithoutDot]);
×
275
        }
276

UNCOV
277
        return $processedRenderable;
×
278
    }
279

280
    private function processErrorMessage(Context\ValueResolutionContext $context, Extbase\Error\Result $result): mixed
×
281
    {
282
        $error = $result->getFirstError();
×
283

284
        // Early return if no error is attached to result
285
        if ($error === false) {
×
286
            return null;
×
287
        }
288

UNCOV
289
        $translationResult = $this->viewHelperInvoker->invoke(
×
UNCOV
290
            $context->viewModel->renderingContext,
×
UNCOV
291
            Form\ViewHelpers\TranslateElementErrorViewHelper::class,
×
UNCOV
292
            [
×
UNCOV
293
                'element' => $context->renderable,
×
294
                'error' => $error,
×
UNCOV
295
            ],
×
296
        );
×
297

298
        return $translationResult->content;
×
299
    }
300

301
    /**
302
     * @param array<string, mixed> $configuration
303
     */
UNCOV
304
    private function processProperty(Context\ValueResolutionContext $context, array $configuration): mixed
×
305
    {
UNCOV
306
        $path = $configuration['path'] ?? null;
×
307

308
        if (!is_string($path)) {
×
UNCOV
309
            return null;
×
310
        }
311

312
        return Extbase\Reflection\ObjectAccess::getProperty($context->renderable, $path);
×
313
    }
314

315
    /**
316
     * @param array<string, mixed> $configuration
317
     */
UNCOV
318
    private function processResult(Extbase\Error\Result $result, array $configuration): mixed
×
319
    {
UNCOV
320
        $propertyPath = $configuration['propertyPath'];
×
321

UNCOV
322
        if (!is_string($propertyPath)) {
×
UNCOV
323
            return $result;
×
324
        }
325

UNCOV
326
        return Extbase\Reflection\ObjectAccess::getProperty($result, $propertyPath);
×
327
    }
328
}
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