• 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/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\Renderable\ViewModel\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

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
                if (Utility\StringUtility::isStringable($value)) {
×
113
                    $value = Utility\StringUtility::processStringable(
×
114
                        $value,
×
115
                        static fn(string $string) => str_replace(self::CONTENT_PLACEHOLDER, $formContent, $string),
×
116
                    );
×
117
                }
118
            });
×
119
        }
120

121
        return $processedData;
×
122
    }
123

124
    /**
125
     * @param array<string|int, mixed> $configuration
126
     * @return array<string|int, mixed>|null
127
     */
128
    private function processRenderable(
×
129
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
130
        array $configuration,
131
        Frontend\ContentObject\ContentObjectRenderer $cObj,
132
        Domain\Renderable\ViewModel\ViewModel $viewModel,
133
    ): ?array {
134
        $processedData = [];
×
135

136
        // Early return on configured "if" condition evaluating to false
137
        if (!$this->checkIf($configuration, $renderable, $cObj, $viewModel)) {
×
138
            return null;
×
139
        }
140

141
        // Merge TS reference (=<) and replace configuration with merged configuration
142
        $this->mergeTypoScriptReferences($configuration, $cObj);
×
143

144
        foreach ($configuration as $key => $value) {
×
145
            $keyWithoutDot = rtrim((string)$key, '.');
×
146
            $keyWithDot = $keyWithoutDot . '.';
×
147

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

151
                if (is_array($resolvedValue)) {
×
152
                    $processedData[$keyWithoutDot] = $resolvedValue;
×
153
                }
154
            }
155

156
            if (!is_string($value)) {
×
157
                continue;
×
158
            }
159

160
            $valueConfiguration = $configuration[$keyWithDot] ?? [];
×
161
            $contentObject = $cObj->getContentObject($value);
×
162

UNCOV
163
            if (!is_array($valueConfiguration)) {
×
164
                $valueConfiguration = [];
×
165
            }
166

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

UNCOV
184
                $this->contextStack->push($context);
×
185

186
                try {
UNCOV
187
                    $resolvedValue = $cObj->render($contentObject, $valueConfiguration);
×
188
                } finally {
189
                    $this->contextStack->pop();
×
190
                }
191

192
                if ($this->valueCollector->has($resolvedValue)) {
×
UNCOV
193
                    $resolvedValue = $this->valueCollector->load($resolvedValue);
×
194
                }
195
            } else {
196
                $resolvedValue = $value;
×
197
            }
198

199
            // Skip further processing if processed value is not a string (all COR related methods require a string value)
UNCOV
200
            if (!Utility\StringUtility::isStringable($resolvedValue)) {
×
UNCOV
201
                $processedData[$keyWithoutDot] = $resolvedValue;
×
202
                continue;
×
203
            }
204

205
            // Skip value if a configured "if" evaluates to false
206
            if (is_array($valueConfiguration['if.'] ?? null)) {
×
UNCOV
207
                $valueConfiguration['if.']['value'] ??= (string)$resolvedValue;
×
208

UNCOV
209
                if (!$this->checkIf($valueConfiguration, $renderable, $cObj, $viewModel)) {
×
210
                    continue;
×
211
                }
212
            }
213

UNCOV
214
            $processedData[$keyWithoutDot] = $resolvedValue;
×
215
        }
216

UNCOV
217
        return $processedData;
×
218
    }
219

220
    /**
221
     * @param array<string|int, mixed> $configuration
222
     */
223
    private function mergeTypoScriptReferences(
×
224
        array &$configuration,
225
        Frontend\ContentObject\ContentObjectRenderer $cObj,
226
    ): void {
227
        $processedKeys = [];
×
228

UNCOV
229
        foreach ($configuration as $key => $value) {
×
230
            if (in_array($key, $processedKeys, true)) {
×
231
                continue;
×
232
            }
233

234
            $keyWithoutDot = rtrim((string)$key, '.');
×
UNCOV
235
            $keyWithDot = $keyWithoutDot . '.';
×
236

237
            if (array_key_exists($keyWithDot, $configuration) && is_array($configuration[$keyWithDot])) {
×
238
                $this->mergeTypoScriptReferences($configuration[$keyWithDot], $cObj);
×
239
            }
240

241
            if (!array_key_exists($keyWithoutDot, $configuration)) {
×
242
                continue;
×
243
            }
244

245
            $mergedConfig = $cObj->mergeTSRef(
×
246
                [
×
247
                    $keyWithoutDot => $configuration[$keyWithoutDot] ?? '',
×
UNCOV
248
                    $keyWithDot => $configuration[$keyWithDot] ?? [],
×
249
                ],
×
UNCOV
250
                $keyWithoutDot,
×
251
            );
×
252

UNCOV
253
            $configuration[$keyWithoutDot] = $mergedConfig[$keyWithoutDot];
×
254

255
            if ($mergedConfig[$keyWithDot] !== []) {
×
256
                $configuration[$keyWithDot] = $mergedConfig[$keyWithDot];
×
257
            }
258

UNCOV
259
            $processedKeys[] = $keyWithoutDot;
×
UNCOV
260
            $processedKeys[] = $keyWithDot;
×
261
        }
262
    }
263

264
    /**
265
     * @param array<string|int, mixed> $configuration
266
     */
UNCOV
267
    private function checkIf(
×
268
        array &$configuration,
269
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
270
        Frontend\ContentObject\ContentObjectRenderer $cObj,
271
        Domain\Renderable\ViewModel\ViewModel $viewModel,
272
    ): bool {
273
        if (!is_array($configuration['if.'] ?? null)) {
×
UNCOV
274
            return true;
×
275
        }
276

277
        $cObjTemp = clone $cObj;
×
278

279
        if (is_string($configuration['if.']['currentValue'] ?? null) || is_array($configuration['if.']['currentValue.'] ?? null)) {
×
280
            $processedValue = $this->processRenderable(
×
281
                $renderable,
×
282
                [
×
283
                    'currentValue' => $configuration['if.']['currentValue'] ?? '',
×
284
                    'currentValue.' => $configuration['if.']['currentValue.'] ?? [],
×
UNCOV
285
                ],
×
286
                $cObj,
×
UNCOV
287
                $viewModel,
×
288
            );
×
289

UNCOV
290
            $cObjTemp->setCurrentVal($processedValue['currentValue'] ?? null);
×
291

292
            unset($configuration['if.']['currentValue'], $configuration['if.']['currentValue.']);
×
293
        }
294

295
        if (!$cObjTemp->checkIf($configuration['if.'])) {
×
UNCOV
296
            return false;
×
297
        }
298

UNCOV
299
        unset($configuration['if.']);
×
300

UNCOV
301
        return true;
×
302
    }
303
}
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