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

CPS-IT / handlebars-forms / 22296369608

23 Feb 2026 07:13AM UTC coverage: 0.0%. Remained the same
22296369608

push

github

eliashaeussler
Initial commit

0 of 447 new or added lines in 21 files covered. (0.0%)

0 of 447 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/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\Renderer;
19

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

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

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

NEW
46
    public function render(): string
×
47
    {
NEW
48
        $view = $this->resolveViewFromConfiguration() ?? $this->resolveDefaultView();
×
NEW
49
        $view->assign('form', $this->formRuntime);
×
50

NEW
51
        match ($this->typo3Version->getMajorVersion()) {
×
NEW
52
            13 => $this->triggerBeforeRenderingHook(),
×
NEW
53
            14 => $this->triggerBeforeRenderableIsRenderedEvent(),
×
NEW
54
            default => null,
×
NEW
55
        };
×
56

NEW
57
        if ($view instanceof View\HandlebarsView) {
×
NEW
58
            $templateName = $view->getTemplateName() ?? $this->formRuntime->getTemplateName();
×
59
        } else {
NEW
60
            $templateName = $this->formRuntime->getTemplateName();
×
61
        }
62

NEW
63
        return $view->render($templateName);
×
64
    }
65

NEW
66
    private function resolveViewFromConfiguration(): ?View\HandlebarsView
×
67
    {
NEW
68
        $request = $this->formRuntime->getRequest();
×
NEW
69
        $contentObjectRenderer = $request->getAttribute('currentContentObject');
×
70

71
        // Early return when outside of content object rendering
NEW
72
        if (!($contentObjectRenderer instanceof Frontend\ContentObject\ContentObjectRenderer)) {
×
NEW
73
            return null;
×
74
        }
75

76
        // Resolve form configuration from plugin configuration
NEW
77
        $pluginConfiguration = $this->configurationManager->getConfiguration(
×
NEW
78
            Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
×
NEW
79
        );
×
NEW
80
        $formConfiguration = $pluginConfiguration['handlebarsForms'] ?? null;
×
81

82
        // Early return if no handlebars configuration is available
NEW
83
        if (!is_array($formConfiguration)) {
×
NEW
84
            return null;
×
85
        }
86

87
        // HANDLEBARSTEMPLATE content object requires TypoScript configuration, so let's convert early
NEW
88
        $typoScriptConfiguration = $this->typoScriptService->convertPlainArrayToTypoScriptArray($formConfiguration);
×
89

90
        // Resolve TypoScript configuration based on form identifier
NEW
91
        $resolvedConfiguration = [];
×
NEW
92
        $possibleConfigurationKeys = [
×
93
            // Fallback
NEW
94
            'default',
×
95
            // Unique form identifier
NEW
96
            $this->formRuntime->getIdentifier(),
×
97
            // Original form identifier
NEW
98
            $this->formRuntime->getRenderingOptions()['_originalIdentifier'],
×
99
            // Form persistence identifier
NEW
100
            $this->formRuntime->getFormDefinition()->getPersistenceIdentifier(),
×
NEW
101
        ];
×
NEW
102
        foreach ($possibleConfigurationKeys as $possibleConfigurationKey) {
×
NEW
103
            if (\array_key_exists($possibleConfigurationKey . '.', $typoScriptConfiguration)) {
×
NEW
104
                Core\Utility\ArrayUtility::mergeRecursiveWithOverrule(
×
NEW
105
                    $resolvedConfiguration,
×
NEW
106
                    $typoScriptConfiguration[$possibleConfigurationKey . '.'],
×
NEW
107
                );
×
108
            }
109
        }
110

NEW
111
        return new View\HandlebarsView(
×
NEW
112
            $contentObjectRenderer,
×
NEW
113
            $this->typoScriptService,
×
NEW
114
            $resolvedConfiguration,
×
NEW
115
            $request,
×
NEW
116
        );
×
117
    }
118

NEW
119
    private function resolveDefaultView(): Core\View\ViewInterface
×
120
    {
NEW
121
        $renderingOptions = $this->formRuntime->getRenderingOptions();
×
NEW
122
        $viewFactoryData = new Core\View\ViewFactoryData(
×
NEW
123
            templateRootPaths: $renderingOptions['templateRootPaths'] ?? [],
×
NEW
124
            partialRootPaths: $renderingOptions['partialRootPaths'] ?? [],
×
NEW
125
            layoutRootPaths: $renderingOptions['layoutRootPaths'] ?? [],
×
NEW
126
            request: $this->formRuntime->getRequest(),
×
NEW
127
        );
×
128

NEW
129
        return $this->viewFactory->create($viewFactoryData);
×
130
    }
131

132
    /**
133
     * @todo Remove once support for TYPO3 v13 is dropped
134
     */
NEW
135
    private function triggerBeforeRenderingHook(): void
×
136
    {
NEW
137
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'] ?? [] as $className) {
×
138
            /* @phpstan-ignore argument.templateType */
NEW
139
            $hookObj = Core\Utility\GeneralUtility::makeInstance($className);
×
NEW
140
            if (method_exists($hookObj, 'beforeRendering')) {
×
NEW
141
                $hookObj->beforeRendering($this->formRuntime, $this->formRuntime->getFormDefinition());
×
142
            }
143
        }
144
    }
145

146
    /**
147
     * @todo Enable once support for TYPO3 v14 is added
148
     */
NEW
149
    private function triggerBeforeRenderableIsRenderedEvent(): void
×
150
    {
151
        // $this->eventDispatcher->dispatch(
152
        //     new Form\Event\BeforeRenderableIsRenderedEvent($this->formRuntime->getFormDefinition(), $this->formRuntime),
153
        // );
NEW
154
    }
×
155
}
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