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

CPS-IT / handlebars-forms / 23788866234

31 Mar 2026 08:52AM UTC coverage: 0.659% (-0.06%) from 0.72%
23788866234

push

github

web-flow
Merge pull request #18 from CPS-IT/feature/view-models

0 of 163 new or added lines in 33 files covered. (0.0%)

7 of 1062 relevant lines covered (0.66%)

0.03 hits per line

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

0.0
/Classes/DataProcessing/ProcessFormProcessor.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\DataProcessing;
19

20
use CPSIT\Typo3HandlebarsForms\ContentObject;
21
use CPSIT\Typo3HandlebarsForms\Domain;
22
use CPSIT\Typo3HandlebarsForms\Utility;
23
use Psr\Http\Message;
24
use Psr\Log;
25
use Symfony\Component\DependencyInjection;
26
use TYPO3\CMS\Fluid;
27
use TYPO3\CMS\Form;
28
use TYPO3\CMS\Frontend;
29
use TYPO3Fluid\Fluid as FluidStandalone;
30

31
/**
32
 * ProcessFormProcessor
33
 *
34
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
35
 * @license GPL-2.0-or-later
36
 */
37
#[DependencyInjection\Attribute\AutoconfigureTag('data.processor', ['identifier' => 'process-form'])]
38
final readonly class ProcessFormProcessor implements Frontend\ContentObject\DataProcessorInterface
39
{
40
    private const CONTENT_PLACEHOLDER = '###FORM_CONTENT###';
41

42
    public function __construct(
×
43
        private Log\LoggerInterface $logger,
44
        private Fluid\Core\Rendering\RenderingContextFactory $renderingContextFactory,
45
        private Domain\ViewModel\Builder\FormViewModelBuilder $formRenderableProcessor,
46
        private ContentObject\Context\ValueCollector $valueCollector,
47
        private ContentObject\Context\ContextStack $contextStack,
48
    ) {}
×
49

50
    /**
51
     * @param array<string|int, mixed> $contentObjectConfiguration
52
     * @param array<string|int, mixed> $processorConfiguration
53
     * @param array<string|int, mixed> $processedData
54
     * @return array<string|int, mixed>
55
     */
56
    public function process(
×
57
        Frontend\ContentObject\ContentObjectRenderer $cObj,
58
        array $contentObjectConfiguration,
59
        array $processorConfiguration,
60
        array $processedData,
61
    ): array {
62
        $formRuntime = $contentObjectConfiguration['variables.']['form'] ?? null;
×
63

64
        if (!($formRuntime instanceof Form\Domain\Runtime\FormRuntime)) {
×
65
            $this->logger->error(
×
66
                'Form runtime is not available when trying to process form with plugin uid "{uid}".',
×
67
                ['uid' => $cObj->data['uid'] ?? '(unknown)'],
×
68
            );
×
69

70
            return $processedData;
×
71
        }
72

73
        // Create and prepare Fluid rendering context
74
        $renderingContext = $this->renderingContextFactory->create();
×
75
        $renderingContext->setAttribute(Message\ServerRequestInterface::class, $formRuntime->getRequest());
×
76
        $renderingContext->getViewHelperVariableContainer()->addOrUpdate(
×
77
            Form\ViewHelpers\RenderRenderableViewHelper::class,
×
78
            'formRuntime',
×
79
            $formRuntime,
×
80
        );
×
81

82
        // Render form and process renderables as part of the form's renderChildrenClosure.
83
        // Since the final rendered form content (which especially contains all relevant hidden fields)
84
        // is not yet available when processing renderables, we temporarily pass a content placeholder
85
        // for all configured CONTENT values and replace them with the real content value later.
86
        $this->formRenderableProcessor->build(
×
87
            $formRuntime,
×
88
            $renderingContext,
×
89
            function (FluidStandalone\Core\ViewHelper\TagBuilder $tagBuilder) use (
×
90
                $cObj,
×
91
                $formRuntime,
×
92
                &$processedData,
×
93
                $processorConfiguration,
×
94
                $renderingContext,
×
95
                &$tag,
×
96
            ) {
×
97
                $tag = $tagBuilder;
×
98
                $tag->setContent(self::CONTENT_PLACEHOLDER);
×
99

NEW
100
                $viewModel = new Domain\ViewModel\StandaloneTagViewModel($formRuntime, $tag);
×
NEW
101
                $processedData = $this->processRenderable(
×
NEW
102
                    $formRuntime,
×
NEW
103
                    $processorConfiguration,
×
NEW
104
                    $renderingContext,
×
NEW
105
                    $formRuntime,
×
NEW
106
                    $cObj,
×
NEW
107
                    $viewModel,
×
NEW
108
                ) ?? [];
×
109

110
                return '';
×
111
            },
×
112
        );
×
113

114
        $formContent = $tag?->getContent();
×
115

116
        // Replace content placeholder with final rendered form content
117
        if ($formContent !== null) {
×
118
            array_walk_recursive($processedData, static function (&$value) use ($formContent) {
×
119
                if (Utility\StringUtility::isStringable($value)) {
×
120
                    $value = Utility\StringUtility::processStringable(
×
121
                        $value,
×
122
                        static fn(string $string) => str_replace(self::CONTENT_PLACEHOLDER, $formContent, $string),
×
123
                    );
×
124
                }
125
            });
×
126
        }
127

128
        return $processedData;
×
129
    }
130

131
    /**
132
     * @param array<string|int, mixed> $configuration
133
     * @return array<string|int, mixed>|null
134
     */
135
    private function processRenderable(
×
136
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
137
        array $configuration,
138
        Fluid\Core\Rendering\RenderingContext $renderingContext,
139
        Form\Domain\Runtime\FormRuntime $formRuntime,
140
        Frontend\ContentObject\ContentObjectRenderer $cObj,
141
        Domain\ViewModel\ViewModel $viewModel,
142
    ): ?array {
143
        $processedData = [];
×
144

145
        // Early return on configured "if" condition evaluating to false
NEW
146
        if (!$this->checkIf($configuration, $renderable, $renderingContext, $formRuntime, $cObj, $viewModel)) {
×
147
            return null;
×
148
        }
149

150
        // Merge TS reference (=<) and replace configuration with merged configuration
151
        $this->mergeTypoScriptReferences($configuration, $cObj);
×
152

153
        foreach ($configuration as $key => $value) {
×
154
            $keyWithoutDot = rtrim((string)$key, '.');
×
155
            $keyWithDot = $keyWithoutDot . '.';
×
156

157
            if (is_array($value) && !array_key_exists($keyWithoutDot, $processedData)) {
×
NEW
158
                $resolvedValue = $this->processRenderable($renderable, $value, $renderingContext, $formRuntime, $cObj, $viewModel);
×
159

160
                if (is_array($resolvedValue)) {
×
161
                    $processedData[$keyWithoutDot] = $resolvedValue;
×
162
                }
163
            }
164

165
            if (!is_string($value)) {
×
166
                continue;
×
167
            }
168

169
            $valueConfiguration = $configuration[$keyWithDot] ?? [];
×
170
            $contentObject = $cObj->getContentObject($value);
×
171

172
            if (!is_array($valueConfiguration)) {
×
173
                $valueConfiguration = [];
×
174
            }
175

176
            // Resolve configured value
177
            if ($contentObject !== null) {
×
178
                $context = new ContentObject\Context\ValueResolutionContext(
×
179
                    $renderable,
×
180
                    $viewModel,
×
NEW
181
                    $renderingContext,
×
182
                    $formRuntime,
×
183
                    fn(
×
184
                        array $contextConfiguration,
×
185
                        ?Form\Domain\Model\Renderable\RootRenderableInterface $contextRenderable = null,
×
NEW
186
                        ?Domain\ViewModel\ViewModel $contextViewModel = null,
×
187
                    ) => $this->processRenderable(
×
188
                        $contextRenderable ?? $renderable,
×
189
                        $contextConfiguration,
×
NEW
190
                        $renderingContext,
×
191
                        $formRuntime,
×
192
                        $cObj,
×
193
                        $contextViewModel ?? $viewModel,
×
194
                    ),
×
195
                );
×
196

197
                $this->contextStack->push($context);
×
198

199
                try {
200
                    $resolvedValue = $cObj->render($contentObject, $valueConfiguration);
×
201
                } finally {
202
                    $this->contextStack->pop();
×
203
                }
204

205
                if ($this->valueCollector->has($resolvedValue)) {
×
206
                    $resolvedValue = $this->valueCollector->load($resolvedValue);
×
207
                }
208
            } else {
209
                $resolvedValue = $value;
×
210
            }
211

212
            // Skip further processing if processed value is not a string (all COR related methods require a string value)
213
            if (!Utility\StringUtility::isStringable($resolvedValue)) {
×
214
                $processedData[$keyWithoutDot] = $resolvedValue;
×
215
                continue;
×
216
            }
217

218
            // Skip value if a configured "if" evaluates to false
219
            if (is_array($valueConfiguration['if.'] ?? null)) {
×
220
                $valueConfiguration['if.']['value'] ??= (string)$resolvedValue;
×
221

NEW
222
                if (!$this->checkIf($valueConfiguration, $renderable, $renderingContext, $formRuntime, $cObj, $viewModel)) {
×
223
                    continue;
×
224
                }
225
            }
226

227
            $processedData[$keyWithoutDot] = $resolvedValue;
×
228
        }
229

230
        return $processedData;
×
231
    }
232

233
    /**
234
     * @param array<string|int, mixed> $configuration
235
     */
236
    private function mergeTypoScriptReferences(
×
237
        array &$configuration,
238
        Frontend\ContentObject\ContentObjectRenderer $cObj,
239
    ): void {
240
        $processedKeys = [];
×
241

242
        foreach ($configuration as $key => $value) {
×
243
            if (in_array($key, $processedKeys, true)) {
×
244
                continue;
×
245
            }
246

247
            $keyWithoutDot = rtrim((string)$key, '.');
×
248
            $keyWithDot = $keyWithoutDot . '.';
×
249

250
            if (array_key_exists($keyWithDot, $configuration) && is_array($configuration[$keyWithDot])) {
×
251
                $this->mergeTypoScriptReferences($configuration[$keyWithDot], $cObj);
×
252
            }
253

254
            if (!array_key_exists($keyWithoutDot, $configuration)) {
×
255
                continue;
×
256
            }
257

258
            $mergedConfig = $cObj->mergeTSRef(
×
259
                [
×
260
                    $keyWithoutDot => $configuration[$keyWithoutDot] ?? '',
×
261
                    $keyWithDot => $configuration[$keyWithDot] ?? [],
×
262
                ],
×
263
                $keyWithoutDot,
×
264
            );
×
265

266
            $configuration[$keyWithoutDot] = $mergedConfig[$keyWithoutDot];
×
267

268
            if ($mergedConfig[$keyWithDot] !== []) {
×
269
                $configuration[$keyWithDot] = $mergedConfig[$keyWithDot];
×
270
            }
271

272
            $processedKeys[] = $keyWithoutDot;
×
273
            $processedKeys[] = $keyWithDot;
×
274
        }
275
    }
276

277
    /**
278
     * @param array<string|int, mixed> $configuration
279
     */
280
    private function checkIf(
×
281
        array &$configuration,
282
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
283
        Fluid\Core\Rendering\RenderingContext $renderingContext,
284
        Form\Domain\Runtime\FormRuntime $formRuntime,
285
        Frontend\ContentObject\ContentObjectRenderer $cObj,
286
        Domain\ViewModel\ViewModel $viewModel,
287
    ): bool {
288
        if (!is_array($configuration['if.'] ?? null)) {
×
289
            return true;
×
290
        }
291

292
        $cObjTemp = clone $cObj;
×
293

294
        if (is_string($configuration['if.']['currentValue'] ?? null) || is_array($configuration['if.']['currentValue.'] ?? null)) {
×
295
            $processedValue = $this->processRenderable(
×
296
                $renderable,
×
297
                [
×
298
                    'currentValue' => $configuration['if.']['currentValue'] ?? '',
×
299
                    'currentValue.' => $configuration['if.']['currentValue.'] ?? [],
×
300
                ],
×
NEW
301
                $renderingContext,
×
302
                $formRuntime,
×
303
                $cObj,
×
304
                $viewModel,
×
305
            );
×
306

307
            $cObjTemp->setCurrentVal($processedValue['currentValue'] ?? null);
×
308

309
            unset($configuration['if.']['currentValue'], $configuration['if.']['currentValue.']);
×
310
        }
311

312
        if (!$cObjTemp->checkIf($configuration['if.'])) {
×
313
            return false;
×
314
        }
315

316
        unset($configuration['if.']);
×
317

318
        return true;
×
319
    }
320
}
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