Repository navigation
Expand file tree
/
Copy pathHandlebarsFormRenderer.php
More file actions
183 lines (156 loc) · 6.65 KB
/
Copy pathHandlebarsFormRenderer.php
File metadata and controls
183 lines (156 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS extension "handlebars_forms".
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace CPSIT\Typo3HandlebarsForms\Domain\Renderer;
use CPSIT\Typo3Handlebars\View;
use Psr\EventDispatcher;
use Symfony\Component\DependencyInjection;
use TYPO3\CMS\Core;
use TYPO3\CMS\Extbase;
use TYPO3\CMS\Form;
use TYPO3\CMS\Frontend;
/**
* HandlebarsFormRenderer
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
#[DependencyInjection\Attribute\Autoconfigure(public: true, shared: false)]
final class HandlebarsFormRenderer extends Form\Domain\Renderer\AbstractElementRenderer
{
private readonly Core\Information\Typo3Version $typo3Version;
public function __construct(
private readonly Extbase\Configuration\ConfigurationManagerInterface $configurationManager,
private readonly View\HandlebarsViewFactory $viewFactory,
private readonly Core\TypoScript\TypoScriptService $typoScriptService,
private readonly EventDispatcher\EventDispatcherInterface $eventDispatcher,
) {
$this->typo3Version = new Core\Information\Typo3Version();
}
public function render(): string
{
$this->disableCacheOnSubmit();
$view = $this->resolveViewFromConfiguration() ?? $this->resolveDefaultView();
$view->assign('form', $this->formRuntime);
match ($this->typo3Version->getMajorVersion()) {
13 => $this->triggerBeforeRenderingHook(),
14 => $this->triggerBeforeRenderableIsRenderedEvent(),
default => null,
};
if ($view instanceof View\HandlebarsView) {
$templateName = $view->getTemplateName() ?? $this->formRuntime->getTemplateName();
} else {
$templateName = $this->formRuntime->getTemplateName();
}
return $view->render($templateName);
}
private function resolveViewFromConfiguration(): ?View\HandlebarsView
{
$request = $this->formRuntime->getRequest();
$contentObjectRenderer = $request->getAttribute('currentContentObject');
// Early return when outside of content object rendering
if (!($contentObjectRenderer instanceof Frontend\ContentObject\ContentObjectRenderer)) {
return null;
}
// Resolve form configuration from plugin configuration
$pluginConfiguration = $this->configurationManager->getConfiguration(
Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
);
$formConfiguration = $pluginConfiguration['handlebarsForms'] ?? null;
// Early return if no handlebars configuration is available
if (!is_array($formConfiguration)) {
return null;
}
// HANDLEBARSTEMPLATE content object requires TypoScript configuration, so let's convert early
$typoScriptConfiguration = $this->typoScriptService->convertPlainArrayToTypoScriptArray($formConfiguration);
// Resolve TypoScript configuration based on form identifier
$resolvedConfiguration = [];
$possibleConfigurationKeys = [
// Fallback
'default',
// Unique form identifier
$this->formRuntime->getIdentifier(),
// Original form identifier
$this->formRuntime->getRenderingOptions()['_originalIdentifier'],
// Form persistence identifier
$this->formRuntime->getFormDefinition()->getPersistenceIdentifier(),
];
foreach ($possibleConfigurationKeys as $possibleConfigurationKey) {
if (is_string($possibleConfigurationKey) &&
is_array($typoScriptConfiguration[$possibleConfigurationKey . '.'] ?? null)
) {
Core\Utility\ArrayUtility::mergeRecursiveWithOverrule(
$resolvedConfiguration,
$typoScriptConfiguration[$possibleConfigurationKey . '.'],
);
}
}
return new View\HandlebarsView(
$contentObjectRenderer,
$this->typoScriptService,
$resolvedConfiguration,
$request,
);
}
private function resolveDefaultView(): Core\View\ViewInterface
{
$renderingOptions = $this->formRuntime->getRenderingOptions();
$templateRootPaths = $renderingOptions['templateRootPaths'] ?? [];
$partialRootPaths = $renderingOptions['partialRootPaths'] ?? [];
$layoutRootPaths = $renderingOptions['layoutRootPaths'] ?? [];
if (!is_array($templateRootPaths)) {
$templateRootPaths = null;
}
if (!is_array($partialRootPaths)) {
$partialRootPaths = null;
}
if (!is_array($layoutRootPaths)) {
$layoutRootPaths = null;
}
$viewFactoryData = new Core\View\ViewFactoryData(
templateRootPaths: $templateRootPaths,
partialRootPaths: $partialRootPaths,
layoutRootPaths: $layoutRootPaths,
request: $this->formRuntime->getRequest(),
);
return $this->viewFactory->create($viewFactoryData);
}
private function disableCacheOnSubmit(): void
{
$request = $this->formRuntime->getRequest();
$cacheInstruction = $request->getAttribute('frontend.cache.instruction');
if ($cacheInstruction instanceof Frontend\Cache\CacheInstruction && $request->getMethod() === 'POST') {
$cacheInstruction->disableCache('EXT:handlebars_forms: Rendering of submitted forms must not be cached.');
}
}
/**
* @todo Remove once support for TYPO3 v13 is dropped
*/
private function triggerBeforeRenderingHook(): void
{
/** @var class-string $className */
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'] ?? [] as $className) {
$hookObj = Core\Utility\GeneralUtility::makeInstance($className);
if (method_exists($hookObj, 'beforeRendering')) {
$hookObj->beforeRendering($this->formRuntime, $this->formRuntime->getFormDefinition());
}
}
}
private function triggerBeforeRenderableIsRenderedEvent(): void
{
$this->eventDispatcher->dispatch(
new Form\Event\BeforeRenderableIsRenderedEvent($this->formRuntime->getFormDefinition(), $this->formRuntime),
);
}
}