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

CPS-IT / handlebars-forms / 23132838979

16 Mar 2026 07:36AM UTC coverage: 0.0%. Remained the same
23132838979

Pull #10

github

web-flow
Merge ad90b5020 into fdb00737e
Pull Request #10: [FEATURE] Convert value resolvers to context-aware content objects

0 of 88 new or added lines in 11 files covered. (0.0%)

2 existing lines in 1 file now uncovered.

0 of 696 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/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 DevTheorem\Handlebars;
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

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

50
    /**
51
     * @param array<string, mixed> $contentObjectConfiguration
52
     * @param array<string, mixed> $processorConfiguration
53
     * @param array<string, mixed> $processedData
54
     * @return array<string, 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

100
                $viewModel = new Domain\Renderable\ViewModel\ViewModel($renderingContext, null, $tag);
×
101
                $processedData = $this->processRenderable($formRuntime, $processorConfiguration, $cObj, $viewModel) ?? [];
×
102

103
                return '';
×
104
            },
×
105
        );
×
106

107
        $formContent = $tag?->getContent();
×
108

109
        // Replace content placeholder with final rendered form content
110
        if ($formContent !== null) {
×
111
            array_walk_recursive($processedData, static function (&$value) use ($formContent) {
×
112
                $isSafeString = false;
×
113

114
                if ($value instanceof Handlebars\SafeString) {
×
115
                    $isSafeString = true;
×
116
                    $value = (string)$value;
×
117
                }
118

119
                if (is_string($value)) {
×
120
                    $value = str_replace(self::CONTENT_PLACEHOLDER, $formContent, $value);
×
121
                }
122

123
                if ($isSafeString) {
×
124
                    $value = new Handlebars\SafeString($value);
×
125
                }
126
            });
×
127
        }
128

129
        return $processedData;
×
130
    }
131

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

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

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

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

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

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

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

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

171
            // Resolve configured value
NEW
172
            if ($contentObject !== null) {
×
NEW
173
                $context = new ContentObject\Context\ValueResolutionContext(
×
174
                    $renderable,
×
175
                    $viewModel,
×
NEW
176
                    fn(
×
NEW
177
                        array $contextConfiguration,
×
NEW
178
                        ?Form\Domain\Model\Renderable\RootRenderableInterface $contextRenderable = null,
×
NEW
179
                        ?Domain\Renderable\ViewModel\ViewModel $contextViewModel = null,
×
NEW
180
                    ) => $this->processRenderable(
×
NEW
181
                        $contextRenderable ?? $renderable,
×
NEW
182
                        $contextConfiguration,
×
NEW
183
                        $cObj,
×
NEW
184
                        $contextViewModel ?? $viewModel,
×
185
                    ),
×
186
                );
×
187

NEW
188
                $this->contextStack->push($context);
×
189

190
                try {
NEW
191
                    $resolvedValue = $cObj->render($contentObject, $valueConfiguration);
×
192
                } finally {
NEW
193
                    $this->contextStack->pop();
×
194
                }
195

NEW
196
                if ($this->valueCollector->has($resolvedValue)) {
×
NEW
197
                    $resolvedValue = $this->valueCollector->load($resolvedValue);
×
198
                }
199
            } else {
200
                $resolvedValue = $value;
×
201
            }
202

203
            // Skip further processing if processed value is not a string (all COR related methods require a string value)
204
            if (!is_string($resolvedValue)) {
×
205
                $processedData[$keyWithoutDot] = $resolvedValue;
×
206
                continue;
×
207
            }
208

209
            // Process value with stdWrap
210
            if (is_array($valueConfiguration['stdWrap.'] ?? null)) {
×
211
                $resolvedValue = $cObj->stdWrap($resolvedValue, $valueConfiguration['stdWrap.']);
×
212
            }
213

214
            // Skip value if a configured "if" evaluates to false
215
            if (is_array($valueConfiguration['if.'] ?? null)) {
×
216
                $valueConfiguration['if.']['value'] ??= $resolvedValue;
×
217

218
                if (!$this->checkIf($valueConfiguration, $renderable, $cObj, $viewModel)) {
×
219
                    continue;
×
220
                }
221
            }
222

223
            // Strings can be considered safe, since the relevant escaping is already performed
224
            // in the view helpers and/or TagBuilder instances when adding attributes
225
            if (is_string($resolvedValue)) {
×
226
                $resolvedValue = new Handlebars\SafeString($resolvedValue);
×
227
            }
228

229
            $processedData[$keyWithoutDot] = $resolvedValue;
×
230
        }
231

232
        return $processedData;
×
233
    }
234

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

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

249
            $keyWithoutDot = rtrim($key, '.');
×
250
            $keyWithDot = $keyWithoutDot . '.';
×
251

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

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

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

268
            $configuration[$keyWithoutDot] = $mergedConfig[$keyWithoutDot];
×
269

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

274
            $processedKeys[] = $keyWithoutDot;
×
275
            $processedKeys[] = $keyWithDot;
×
276
        }
277
    }
278

279
    /**
280
     * @param array<string, mixed> $configuration
281
     */
282
    private function checkIf(
×
283
        array &$configuration,
284
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
285
        Frontend\ContentObject\ContentObjectRenderer $cObj,
286
        Domain\Renderable\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
                ],
×
301
                $cObj,
×
302
                $viewModel,
×
303
            );
×
304

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

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

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

314
        unset($configuration['if.']);
×
315

316
        return true;
×
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