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

CPS-IT / handlebars-forms / 35970439293

24 Sep 2026 07:35AM UTC coverage: 0.513%. Remained the same
35970439293

push

github

web-flow
Merge pull request #90 from CPS-IT/fix/replace

[BUGFIX] Replace content placeholder in strings and Stringables only

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

7 of 1365 relevant lines covered (0.51%)

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 $formViewModelBuilder,
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->formViewModelBuilder->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\ViewModel\StandaloneTagViewModel($formRuntime, $tag);
×
101
                $processedData = $this->processRenderable(
×
102
                    $formRuntime,
×
103
                    $processorConfiguration,
×
104
                    $renderingContext,
×
105
                    $formRuntime,
×
106
                    $cObj,
×
107
                    $viewModel,
×
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
                // Only strings may contain the content placeholder, other scalars must not be converted to strings
NEW
120
                if (is_string($value) || $value instanceof \Stringable) {
×
121
                    $value = Utility\StringUtility::processStringable(
×
122
                        $value,
×
123
                        static fn(string $string) => str_replace(self::CONTENT_PLACEHOLDER, $formContent, $string),
×
124
                    );
×
125
                }
126
            });
×
127
        }
128

129
        return $processedData;
×
130
    }
131

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

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

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

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

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

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

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

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

173
            // Unwrap context-aware content object to avoid conversion of collected scalar values to strings
174
            if ($contentObject instanceof ContentObject\Context\ContextAwareContentObject) {
×
175
                $contentObject = $contentObject->contentObject;
×
176
            }
177

178
            if (!is_array($valueConfiguration)) {
×
179
                $valueConfiguration = [];
×
180
            }
181

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

203
                $this->contextStack->push($context);
×
204

205
                try {
206
                    $resolvedValue = $cObj->render($contentObject, $valueConfiguration);
×
207
                } finally {
208
                    $this->contextStack->pop();
×
209
                }
210

211
                if ($this->valueCollector->has($resolvedValue)) {
×
212
                    $resolvedValue = $this->valueCollector->load($resolvedValue);
×
213
                }
214
            } else {
215
                $resolvedValue = $value;
×
216
            }
217

218
            // Skip further processing if processed value is not a string (all COR related methods require a string value)
219
            if (!Utility\StringUtility::isStringable($resolvedValue)) {
×
220
                $processedData[$keyWithoutDot] = $resolvedValue;
×
221
                continue;
×
222
            }
223

224
            // Skip value if a configured "if" evaluates to false
225
            if (is_array($valueConfiguration['if.'] ?? null)) {
×
226
                $valueConfiguration['if.']['value'] ??= (string)$resolvedValue;
×
227

228
                if (!$this->checkIf($valueConfiguration, $renderable, $renderingContext, $formRuntime, $cObj, $viewModel)) {
×
229
                    continue;
×
230
                }
231
            }
232

233
            $processedData[$keyWithoutDot] = $resolvedValue;
×
234
        }
235

236
        return $processedData;
×
237
    }
238

239
    /**
240
     * @param array<string|int, mixed> $configuration
241
     */
242
    private function mergeTypoScriptReferences(
×
243
        array &$configuration,
244
        Frontend\ContentObject\ContentObjectRenderer $cObj,
245
    ): void {
246
        $processedKeys = [];
×
247

248
        foreach ($configuration as $key => $value) {
×
249
            if (in_array($key, $processedKeys, true)) {
×
250
                continue;
×
251
            }
252

253
            $keyWithoutDot = rtrim((string)$key, '.');
×
254
            $keyWithDot = $keyWithoutDot . '.';
×
255

256
            if (array_key_exists($keyWithDot, $configuration) && is_array($configuration[$keyWithDot])) {
×
257
                $this->mergeTypoScriptReferences($configuration[$keyWithDot], $cObj);
×
258
            }
259

260
            if (!is_string($configuration[$keyWithoutDot] ?? null)) {
×
261
                continue;
×
262
            }
263

264
            if (str_starts_with($configuration[$keyWithoutDot], '<')) {
×
265
                $mergedConfig = $cObj->mergeTSRef(
×
266
                    [
×
267
                        $keyWithoutDot => $configuration[$keyWithoutDot],
×
268
                        $keyWithDot => $configuration[$keyWithDot] ?? [],
×
269
                    ],
×
270
                    $keyWithoutDot,
×
271
                );
×
272

273
                // Apply merged config only if a substition was found and applied
274
                if (is_string($mergedConfig[$keyWithoutDot]) && '<' . $mergedConfig[$keyWithoutDot] !== $configuration[$keyWithoutDot]) {
×
275
                    $configuration[$keyWithoutDot] = $mergedConfig[$keyWithoutDot];
×
276
                }
277

278
                if ($mergedConfig[$keyWithDot] !== []) {
×
279
                    $configuration[$keyWithDot] = $mergedConfig[$keyWithDot];
×
280
                }
281
            }
282

283
            $processedKeys[] = $keyWithoutDot;
×
284
            $processedKeys[] = $keyWithDot;
×
285
        }
286
    }
287

288
    /**
289
     * @param array<string|int, mixed> $configuration
290
     */
291
    private function checkIf(
×
292
        array &$configuration,
293
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
294
        Fluid\Core\Rendering\RenderingContext $renderingContext,
295
        Form\Domain\Runtime\FormRuntime $formRuntime,
296
        Frontend\ContentObject\ContentObjectRenderer $cObj,
297
        Domain\ViewModel\ViewModel $viewModel,
298
    ): bool {
299
        if (!is_array($configuration['if.'] ?? null)) {
×
300
            return true;
×
301
        }
302

303
        $cObjTemp = clone $cObj;
×
304

305
        if (is_string($configuration['if.']['currentValue'] ?? null) || is_array($configuration['if.']['currentValue.'] ?? null)) {
×
306
            $processedValue = $this->processRenderable(
×
307
                $renderable,
×
308
                [
×
309
                    'currentValue' => $configuration['if.']['currentValue'] ?? '',
×
310
                    'currentValue.' => $configuration['if.']['currentValue.'] ?? [],
×
311
                ],
×
312
                $renderingContext,
×
313
                $formRuntime,
×
314
                $cObj,
×
315
                $viewModel,
×
316
            );
×
317

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

320
            unset($configuration['if.']['currentValue'], $configuration['if.']['currentValue.']);
×
321
        }
322

323
        if (!$cObjTemp->checkIf($configuration['if.'])) {
×
324
            return false;
×
325
        }
326

327
        unset($configuration['if.']);
×
328

329
        return true;
×
330
    }
331
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc