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

CPS-IT / handlebars-forms / 23136896946

16 Mar 2026 09:31AM UTC coverage: 0.0%. Remained the same
23136896946

Pull #11

github

web-flow
Merge b7e10ec60 into 478f0059f
Pull Request #11: [FEATURE] Introduce `EACH_RENDERABLE` and `PROPERTY` for validation results

0 of 63 new or added lines in 2 files covered. (0.0%)

2 existing lines in 1 file now uncovered.

0 of 732 relevant lines covered (0.0%)

0.0 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 form definition 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 resolver 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(
×
NEW
70
                $context,
×
71
                $validationResults,
×
72
                $outputInstruction,
×
73
                $outputConfiguration ?? [],
×
74
            );
×
75
        }
76

77
        // Resolve complex rendering configuration
78
        if (is_array($outputConfiguration)) {
×
NEW
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, mixed> $configuration
88
     * @return array<string, 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($key, '.');
×
100
            $keyWithDot = $keyWithoutDot . '.';
×
101

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

125
        return $processedData;
×
126
    }
127

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

147
    /**
148
     * @param array<string, mixed> $configuration
149
     * @return array<string, list<array<string, mixed>>>|list<array<string, mixed>>
150
     */
151
    private function processErrors(
×
152
        Context\ValueResolutionContext $context,
153
        Extbase\Error\Result $result,
154
        array $configuration,
155
    ): array {
NEW
156
        $renderable = $context->renderable;
×
UNCOV
157
        $processedErrors = [];
×
158

159
        foreach ($result->getFlattenedErrors() as $propertyPath => $errors) {
×
160
            $currentRenderable = $renderable;
×
161

162
            if ($propertyPath !== '' && $renderable instanceof Form\Domain\Runtime\FormRuntime) {
×
163
                $currentRenderable = $renderable->getFormDefinition()->getElementByIdentifier($propertyPath);
×
164
            }
165

166
            // Skip errors if current renderable could not be resolved
167
            if ($currentRenderable === null) {
×
168
                continue;
×
169
            }
170

171
            $processedResult = [];
×
172

173
            foreach ($errors as $error) {
×
174
                $errorResult = new Extbase\Error\Result();
×
175
                $errorResult->addError($error);
×
176

177
                $processedResult[] = $this->processRenderingConfiguration(
×
NEW
178
                    $context->withRenderable($currentRenderable),
×
179
                    $errorResult,
×
180
                    $configuration,
×
181
                );
×
182
            }
183

184
            $processedErrors[$propertyPath] = $processedResult;
×
185
        }
186

187
        if ($renderable instanceof Form\Domain\Model\Renderable\CompositeRenderableInterface
×
188
            || $renderable instanceof Form\Domain\Runtime\FormRuntime
×
189
        ) {
190
            return $processedErrors;
×
191
        }
192

193
        $firstProcessedError = reset($processedErrors);
×
194

195
        if ($firstProcessedError !== false) {
×
196
            return $firstProcessedError;
×
197
        }
198

199
        return [];
×
200
    }
201

202
    /**
203
     * @param array<string, mixed> $configuration
204
     * @return array<string, array<string, mixed>>
205
     */
NEW
206
    private function processRenderables(
×
207
        Context\ValueResolutionContext $context,
208
        Extbase\Error\Result $result,
209
        array $configuration,
210
    ): array {
NEW
211
        $renderable = $context->renderable;
×
NEW
212
        $processedRenderables = [];
×
213

NEW
214
        foreach ($result->getFlattenedErrors() as $propertyPath => $errors) {
×
NEW
215
            $currentRenderable = $renderable;
×
216

NEW
217
            if ($propertyPath !== '' && $renderable instanceof Form\Domain\Runtime\FormRuntime) {
×
NEW
218
                $currentRenderable = $renderable->getFormDefinition()->getElementByIdentifier($propertyPath);
×
219
            }
220

221
            // Skip errors if current renderable could not be resolved
NEW
222
            if ($currentRenderable === null) {
×
NEW
223
                continue;
×
224
            }
225

226
            // Process validation result first
NEW
227
            $processedRenderable = $this->processRenderingConfiguration(
×
NEW
228
                $context->withRenderable($currentRenderable),
×
NEW
229
                $result->forProperty($propertyPath),
×
NEW
230
                $configuration,
×
NEW
231
                true,
×
NEW
232
            );
×
233

234
            // Normalize rendering configuration for post-processing
NEW
235
            $processedRenderable = $this->normalizeRenderingConfiguration($processedRenderable);
×
236

237
            // Post-process renderable
NEW
238
            $processedRenderables[$propertyPath] = $context->process($processedRenderable, $currentRenderable);
×
239
        }
240

NEW
241
        return $processedRenderables;
×
242
    }
243

244
    /**
245
     * @param array<string, mixed> $processedRenderable
246
     * @return array<string, mixed>
247
     */
NEW
248
    private function normalizeRenderingConfiguration(array $processedRenderable): array
×
249
    {
NEW
250
        foreach ($processedRenderable as $key => $value) {
×
NEW
251
            $keyWithoutDot = rtrim($key, '.');
×
NEW
252
            $keyWithDot = $keyWithoutDot . '.';
×
253

NEW
254
            if (!is_array($value) || $key === $keyWithDot) {
×
NEW
255
                continue;
×
256
            }
257

NEW
258
            if (!array_key_exists($keyWithDot, $processedRenderable)) {
×
NEW
259
                $processedRenderable[$keyWithDot] = $this->normalizeRenderingConfiguration($value);
×
260
            }
261

NEW
262
            $processedRenderable[$keyWithDot] = array_replace_recursive($processedRenderable[$keyWithDot], $value);
×
263

NEW
264
            unset($processedRenderable[$keyWithoutDot]);
×
265
        }
266

NEW
267
        return $processedRenderable;
×
268
    }
269

NEW
270
    private function processErrorMessage(Context\ValueResolutionContext $context, Extbase\Error\Result $result): mixed
×
271
    {
UNCOV
272
        $error = $result->getFirstError();
×
273

274
        // Early return if no error is attached to result
275
        if ($error === false) {
×
276
            return null;
×
277
        }
278

279
        $translationResult = $this->viewHelperInvoker->invoke(
×
NEW
280
            $context->viewModel->renderingContext,
×
281
            Form\ViewHelpers\TranslateElementErrorViewHelper::class,
×
282
            [
×
NEW
283
                'element' => $context->renderable,
×
284
                'error' => $error,
×
285
            ],
×
286
        );
×
287

288
        return $translationResult->content;
×
289
    }
290

291
    /**
292
     * @param array<string, mixed> $configuration
293
     */
NEW
294
    private function processProperty(Context\ValueResolutionContext $context, array $configuration): mixed
×
295
    {
296
        $path = $configuration['path'] ?? null;
×
297

298
        if (!is_string($path)) {
×
299
            return null;
×
300
        }
301

NEW
302
        return Extbase\Reflection\ObjectAccess::getProperty($context->renderable, $path);
×
303
    }
304

305
    /**
306
     * @param array<string, mixed> $configuration
307
     */
NEW
308
    private function processResult(Extbase\Error\Result $result, array $configuration): mixed
×
309
    {
NEW
310
        $propertyPath = $configuration['propertyPath'];
×
311

NEW
312
        if (!is_string($propertyPath)) {
×
NEW
313
            return $result;
×
314
        }
315

NEW
316
        return Extbase\Reflection\ObjectAccess::getProperty($result, $propertyPath);
×
317
    }
318
}
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