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

CPS-IT / handlebars-forms / 22565586275

02 Mar 2026 07:20AM UTC coverage: 0.0%. Remained the same
22565586275

push

github

web-flow
Merge pull request #7 from CPS-IT/renovate/major-github-artifact-actions

0 of 501 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\Domain;
21
use DevTheorem\Handlebars;
22
use Psr\Http\Message;
23
use Psr\Log;
24
use Symfony\Component\DependencyInjection;
25
use TYPO3\CMS\Fluid;
26
use TYPO3\CMS\Form;
27
use TYPO3\CMS\Frontend;
28
use TYPO3Fluid\Fluid as FluidStandalone;
29

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

41
    /**
42
     * @param DependencyInjection\ServiceLocator<Value\ValueResolver> $valueResolvers
43
     */
44
    public function __construct(
×
45
        private Log\LoggerInterface $logger,
46
        private Fluid\Core\Rendering\RenderingContextFactory $renderingContextFactory,
47
        private Domain\Renderable\ViewModel\FormViewModelBuilder $formRenderableProcessor,
48
        #[DependencyInjection\Attribute\AutowireLocator('handlebars_forms.value_resolver', defaultIndexMethod: 'getName')]
49
        private DependencyInjection\ServiceLocator $valueResolvers,
50
    ) {}
×
51

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

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

72
            return $processedData;
×
73
        }
74

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

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

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

105
                return '';
×
106
            },
×
107
        );
×
108

109
        $formContent = $tag?->getContent();
×
110

111
        // Replace content placeholder with final rendered form content
112
        if ($formContent !== null) {
×
113
            array_walk_recursive($processedData, static function (&$value) use ($formContent) {
×
114
                if (is_string($value)) {
×
115
                    $value = str_replace(self::CONTENT_PLACEHOLDER, $formContent, $value);
×
116
                }
117
            });
×
118
        }
119

120
        return $processedData;
×
121
    }
122

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

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

140
        foreach ($configuration as $key => $value) {
×
141
            $keyWithoutDot = rtrim($key, '.');
×
142
            $keyWithDot = $keyWithoutDot . '.';
×
143

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

147
                if (is_array($resolvedValue)) {
×
148
                    $processedData[$keyWithoutDot] = $resolvedValue;
×
149
                }
150
            }
151

152
            if (!is_string($value)) {
×
153
                continue;
×
154
            }
155

156
            $valueConfiguration = $configuration[$keyWithDot] ?? [];
×
157

158
            // Resolve configured value
159
            if ($this->valueResolvers->has($value)) {
×
160
                $resolvedValue = $this->valueResolvers->get($value)->resolve(
×
161
                    $renderable,
×
162
                    $viewModel,
×
163
                    new Value\ValueResolutionContext(
×
164
                        $valueConfiguration,
×
165
                        fn(
×
166
                            array $contextConfiguration,
×
167
                            ?Form\Domain\Model\Renderable\RootRenderableInterface $contextRenderable = null,
×
168
                            ?Domain\Renderable\ViewModel\ViewModel $contextViewModel = null,
×
169
                        ) => $this->processRenderable(
×
170
                            $contextRenderable ?? $renderable,
×
171
                            $contextConfiguration,
×
172
                            $cObj,
×
173
                            $contextViewModel ?? $viewModel,
×
174
                        ),
×
175
                    ),
×
176
                );
×
177
            } else {
178
                $resolvedValue = $value;
×
179
            }
180

181
            // Skip further processing if processed value is not a string (all COR related methods require a string value)
182
            if (!is_string($resolvedValue)) {
×
183
                $processedData[$keyWithoutDot] = $resolvedValue;
×
184
                continue;
×
185
            }
186

187
            // Process value with stdWrap
188
            if (is_array($valueConfiguration['stdWrap.'] ?? null)) {
×
189
                $resolvedValue = $cObj->stdWrap($resolvedValue, $valueConfiguration['stdWrap.']);
×
190
            }
191

192
            // Skip value if a configured "if" evaluates to false
193
            if (is_array($valueConfiguration['if.'] ?? null)) {
×
194
                $valueConfiguration['if.']['value'] ??= $resolvedValue;
×
195

196
                if (!$cObj->checkIf($valueConfiguration['if.'])) {
×
197
                    continue;
×
198
                }
199
            }
200

201
            // Strings can be considered safe, since the relevant escaping is already performed
202
            // in the view helpers and/or TagBuilder instances when adding attributes
203
            if (is_string($resolvedValue)) {
×
204
                $resolvedValue = new Handlebars\SafeString($resolvedValue);
×
205
            }
206

207
            $processedData[$keyWithoutDot] = $resolvedValue;
×
208
        }
209

210
        return $processedData;
×
211
    }
212

213
    /**
214
     * @param array<string, mixed> $configuration
215
     */
216
    private function checkIf(
×
217
        array &$configuration,
218
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
219
        Frontend\ContentObject\ContentObjectRenderer $cObj,
220
        Domain\Renderable\ViewModel\ViewModel $viewModel,
221
    ): bool {
222
        if (!is_array($configuration['if.'] ?? null)) {
×
223
            return true;
×
224
        }
225

226
        $cObjTemp = clone $cObj;
×
227

228
        if (is_string($configuration['if.']['value'] ?? null) && is_array($configuration['if.']['value.'] ?? null)) {
×
229
            $processedValue = $this->processRenderable(
×
230
                $renderable,
×
231
                [
×
232
                    'value' => $configuration['if.']['value'],
×
233
                    'value.' => $configuration['if.']['value.'],
×
234
                ],
×
235
                $cObj,
×
236
                $viewModel,
×
237
            );
×
238

239
            $cObjTemp->setCurrentVal($processedValue['value'] ?? null);
×
240

241
            unset($configuration['if.']['value'], $configuration['if.']['value.']);
×
242
        }
243

244
        if (!$cObjTemp->checkIf($configuration['if.'])) {
×
245
            return false;
×
246
        }
247

248
        unset($configuration['if.']);
×
249

250
        return true;
×
251
    }
252
}
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