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

CPS-IT / handlebars-forms / 25426556929

06 May 2026 09:12AM UTC coverage: 0.548% (-0.008%) from 0.556%
25426556929

Pull #37

github

web-flow
Merge 6bbaf721f into 9b3f4bd22
Pull Request #37: [FEATURE] Add support for TYPO3 v14.3 LTS

0 of 31 new or added lines in 4 files covered. (0.0%)

1 existing line in 1 file now uncovered.

7 of 1277 relevant lines covered (0.55%)

0.02 hits per line

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

0.0
/Classes/Domain/Renderer/HandlebarsFormRenderer.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\Domain\Renderer;
19

20
use CPSIT\Typo3Handlebars\View;
21
use Psr\EventDispatcher;
22
use Symfony\Component\DependencyInjection;
23
use TYPO3\CMS\Core;
24
use TYPO3\CMS\Extbase;
25
use TYPO3\CMS\Form;
26
use TYPO3\CMS\Frontend;
27

28
/**
29
 * HandlebarsFormRenderer
30
 *
31
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
32
 * @license GPL-2.0-or-later
33
 */
34
#[DependencyInjection\Attribute\Autoconfigure(public: true, shared: false)]
35
final class HandlebarsFormRenderer extends Form\Domain\Renderer\AbstractElementRenderer
36
{
37
    private readonly Core\Information\Typo3Version $typo3Version;
38

39
    public function __construct(
×
40
        private readonly Extbase\Configuration\ConfigurationManagerInterface $configurationManager,
41
        private readonly View\HandlebarsViewFactory $viewFactory,
42
        private readonly Core\TypoScript\TypoScriptService $typoScriptService,
43
        private readonly EventDispatcher\EventDispatcherInterface $eventDispatcher,
44
    ) {
45
        $this->typo3Version = new Core\Information\Typo3Version();
×
46
    }
47

48
    public function render(): string
×
49
    {
50
        $this->disableCacheOnSubmit();
×
51

52
        $view = $this->resolveViewFromConfiguration() ?? $this->resolveDefaultView();
×
53
        $view->assign('form', $this->formRuntime);
×
54

55
        match ($this->typo3Version->getMajorVersion()) {
×
56
            13 => $this->triggerBeforeRenderingHook(),
×
57
            14 => $this->triggerBeforeRenderableIsRenderedEvent(),
×
58
            default => null,
×
59
        };
×
60

61
        if ($view instanceof View\HandlebarsView) {
×
62
            $templateName = $view->getTemplateName() ?? $this->formRuntime->getTemplateName();
×
63
        } else {
64
            $templateName = $this->formRuntime->getTemplateName();
×
65
        }
66

67
        return $view->render($templateName);
×
68
    }
69

70
    private function resolveViewFromConfiguration(): ?View\HandlebarsView
×
71
    {
72
        $request = $this->formRuntime->getRequest();
×
73
        $contentObjectRenderer = $request->getAttribute('currentContentObject');
×
74

75
        // Early return when outside of content object rendering
76
        if (!($contentObjectRenderer instanceof Frontend\ContentObject\ContentObjectRenderer)) {
×
77
            return null;
×
78
        }
79

80
        // Resolve form configuration from plugin configuration
81
        $pluginConfiguration = $this->configurationManager->getConfiguration(
×
82
            Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
×
83
        );
×
84
        $formConfiguration = $pluginConfiguration['handlebarsForms'] ?? null;
×
85

86
        // Early return if no handlebars configuration is available
87
        if (!is_array($formConfiguration)) {
×
88
            return null;
×
89
        }
90

91
        // HANDLEBARSTEMPLATE content object requires TypoScript configuration, so let's convert early
92
        $typoScriptConfiguration = $this->typoScriptService->convertPlainArrayToTypoScriptArray($formConfiguration);
×
93

94
        // Resolve TypoScript configuration based on form identifier
95
        $resolvedConfiguration = [];
×
96
        $possibleConfigurationKeys = [
×
97
            // Fallback
98
            'default',
×
99
            // Unique form identifier
100
            $this->formRuntime->getIdentifier(),
×
101
            // Original form identifier
102
            $this->formRuntime->getRenderingOptions()['_originalIdentifier'],
×
103
            // Form persistence identifier
104
            $this->formRuntime->getFormDefinition()->getPersistenceIdentifier(),
×
105
        ];
×
106
        foreach ($possibleConfigurationKeys as $possibleConfigurationKey) {
×
107
            if (is_string($possibleConfigurationKey) &&
×
108
                is_array($typoScriptConfiguration[$possibleConfigurationKey . '.'] ?? null)
×
109
            ) {
110
                Core\Utility\ArrayUtility::mergeRecursiveWithOverrule(
×
111
                    $resolvedConfiguration,
×
112
                    $typoScriptConfiguration[$possibleConfigurationKey . '.'],
×
113
                );
×
114
            }
115
        }
116

117
        return new View\HandlebarsView(
×
118
            $contentObjectRenderer,
×
119
            $this->typoScriptService,
×
120
            $resolvedConfiguration,
×
121
            $request,
×
122
        );
×
123
    }
124

125
    private function resolveDefaultView(): Core\View\ViewInterface
×
126
    {
127
        $renderingOptions = $this->formRuntime->getRenderingOptions();
×
128
        $templateRootPaths = $renderingOptions['templateRootPaths'] ?? [];
×
129
        $partialRootPaths = $renderingOptions['partialRootPaths'] ?? [];
×
130
        $layoutRootPaths = $renderingOptions['layoutRootPaths'] ?? [];
×
131

132
        if (!is_array($templateRootPaths)) {
×
133
            $templateRootPaths = null;
×
134
        }
135
        if (!is_array($partialRootPaths)) {
×
136
            $partialRootPaths = null;
×
137
        }
138
        if (!is_array($layoutRootPaths)) {
×
139
            $layoutRootPaths = null;
×
140
        }
141

142
        $viewFactoryData = new Core\View\ViewFactoryData(
×
143
            templateRootPaths: $templateRootPaths,
×
144
            partialRootPaths: $partialRootPaths,
×
145
            layoutRootPaths: $layoutRootPaths,
×
146
            request: $this->formRuntime->getRequest(),
×
147
        );
×
148

149
        return $this->viewFactory->create($viewFactoryData);
×
150
    }
151

152
    private function disableCacheOnSubmit(): void
×
153
    {
154
        $request = $this->formRuntime->getRequest();
×
155
        $cacheInstruction = $request->getAttribute('frontend.cache.instruction');
×
156

157
        if ($cacheInstruction instanceof Frontend\Cache\CacheInstruction && $request->getMethod() === 'POST') {
×
158
            $cacheInstruction->disableCache('EXT:handlebars_forms: Rendering of submitted forms must not be cached.');
×
159
        }
160
    }
161

162
    /**
163
     * @todo Remove once support for TYPO3 v13 is dropped
164
     */
165
    private function triggerBeforeRenderingHook(): void
×
166
    {
167
        /** @var class-string $className */
168
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'] ?? [] as $className) {
×
169
            $hookObj = Core\Utility\GeneralUtility::makeInstance($className);
×
170

171
            if (method_exists($hookObj, 'beforeRendering')) {
×
172
                $hookObj->beforeRendering($this->formRuntime, $this->formRuntime->getFormDefinition());
×
173
            }
174
        }
175
    }
176

UNCOV
177
    private function triggerBeforeRenderableIsRenderedEvent(): void
×
178
    {
NEW
179
        $this->eventDispatcher->dispatch(
×
NEW
180
            new Form\Event\BeforeRenderableIsRenderedEvent($this->formRuntime->getFormDefinition(), $this->formRuntime),
×
NEW
181
        );
×
182
    }
183
}
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