-
Notifications
You must be signed in to change notification settings - Fork 646
/
FormFrontendController.php
217 lines (194 loc) · 8.34 KB
/
FormFrontendController.php
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* 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 TYPO3\CMS\Form\Controller;
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
use TYPO3\CMS\Core\Service\FlexFormService;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing;
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor;
use TYPO3\CMS\Form\Domain\Configuration\ConfigurationService;
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\FinisherOptionsFlexFormOverridesConverter;
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\FlexFormFinisherOverridesConverterDto;
use TYPO3\CMS\Form\Mvc\Configuration\TypoScriptService;
use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface;
/**
* The frontend controller
*
* Scope: frontend
* @internal
*/
class FormFrontendController extends ActionController
{
/**
* @var \TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface
*/
protected $formPersistenceManager;
/**
* @param \TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface $formPersistenceManager
* @internal
*/
public function injectFormPersistenceManager(FormPersistenceManagerInterface $formPersistenceManager)
{
$this->formPersistenceManager = $formPersistenceManager;
}
/**
* Take the form which should be rendered from the plugin settings
* and overlay the formDefinition with additional data from
* flexform and typoscript settings.
* This method is used directly to display the first page from the
* formDefinition because its cached.
*
* @internal
*/
public function renderAction()
{
$formDefinition = [];
if (!empty($this->settings['persistenceIdentifier'])) {
$formDefinition = $this->formPersistenceManager->load($this->settings['persistenceIdentifier']);
$formDefinition['persistenceIdentifier'] = $this->settings['persistenceIdentifier'];
$formDefinition = $this->overrideByTypoScriptSettings($formDefinition);
$formDefinition = $this->overrideByFlexFormSettings($formDefinition);
$formDefinition = ArrayUtility::setValueByPath($formDefinition, 'renderingOptions._originalIdentifier', $formDefinition['identifier'], '.');
$formDefinition['identifier'] .= '-' . $this->configurationManager->getContentObject()->data['uid'];
}
$this->view->assign('formConfiguration', $formDefinition);
}
/**
* This method is used to display all pages / finishers except the
* first page because its non cached.
*
* @internal
*/
public function performAction()
{
$this->forward('render');
}
/**
* Override the formDefinition with additional data from the Flexform
* settings. For now, only finisher settings are overridable.
*
* @param array $formDefinition
* @return array
*/
protected function overrideByFlexFormSettings(array $formDefinition): array
{
$flexFormData = GeneralUtility::xml2array($this->configurationManager->getContentObject()->data['pi_flexform'] ?? '');
if (!is_array($flexFormData)) {
return $formDefinition;
}
if (isset($formDefinition['finishers'])) {
$prototypeName = $formDefinition['prototypeName'] ?? 'standard';
$configurationService = $this->objectManager->get(ConfigurationService::class);
$prototypeConfiguration = $configurationService->getPrototypeConfiguration($prototypeName);
foreach ($formDefinition['finishers'] as $index => $formFinisherDefinition) {
$finisherIdentifier = $formFinisherDefinition['identifier'];
$sheetIdentifier = $this->getFlexformSheetIdentifier($formDefinition, $prototypeName, $finisherIdentifier);
$flexFormSheetSettings = $this->getFlexFormSettingsFromSheet($flexFormData, $sheetIdentifier);
if ($this->settings['overrideFinishers'] && isset($flexFormSheetSettings['finishers'][$finisherIdentifier])) {
$prototypeFinisherDefinition = $prototypeConfiguration['finishersDefinition'][$finisherIdentifier] ?? [];
$converterDto = GeneralUtility::makeInstance(
FlexFormFinisherOverridesConverterDto::class,
$prototypeFinisherDefinition,
$formFinisherDefinition,
$finisherIdentifier,
$flexFormSheetSettings
);
// Iterate over all `TYPO3.CMS.Form.prototypes.<prototypeName>.finishersDefinition.<finisherIdentifier>.FormEngine.elements` values
GeneralUtility::makeInstance(ArrayProcessor::class, $prototypeFinisherDefinition['FormEngine']['elements'])->forEach(
GeneralUtility::makeInstance(
ArrayProcessing::class,
'modifyFinisherOptionsFromFlexFormOverrides',
'^(.*)(?:(?<!\.TCEforms)\.config\.type|\.section)$',
GeneralUtility::makeInstance(FinisherOptionsFlexFormOverridesConverter::class, $converterDto)
)
);
$formDefinition['finishers'][$index] = $converterDto->getFinisherDefinition();
}
}
}
return $formDefinition;
}
/**
* Every formDefinition setting are overridable by typoscript.
* If the typoscript configuration path
* plugin.tx_form.settings.formDefinitionOverrides.<identifier>
* exists, this settings are merged into the formDefinition.
*
* @param array $formDefinition
* @return array
*/
protected function overrideByTypoScriptSettings(array $formDefinition): array
{
if (
isset($this->settings['formDefinitionOverrides'][$formDefinition['identifier']])
&& !empty($this->settings['formDefinitionOverrides'][$formDefinition['identifier']])
) {
ArrayUtility::mergeRecursiveWithOverrule(
$formDefinition,
$this->settings['formDefinitionOverrides'][$formDefinition['identifier']]
);
$formDefinition = $this->objectManager->get(TypoScriptService::class)
->resolvePossibleTypoScriptConfiguration($formDefinition);
}
return $formDefinition;
}
/**
* @param array $formDefinition
* @param string $prototypeName
* @param string $finisherIdentifier
* @return string
*/
protected function getFlexformSheetIdentifier(
array $formDefinition,
string $prototypeName,
string $finisherIdentifier
): string {
return md5(
implode('', [
$formDefinition['persistenceIdentifier'],
$prototypeName,
$formDefinition['identifier'],
$finisherIdentifier
])
);
}
/**
* @param array $flexForm
* @param string $sheetIdentifier
* @return array
*/
protected function getFlexFormSettingsFromSheet(
array $flexForm,
string $sheetIdentifier
): array {
$sheetData = [];
$sheetData['data'] = array_filter(
$flexForm['data'] ?? [],
function ($key) use ($sheetIdentifier) {
return $key === $sheetIdentifier;
},
ARRAY_FILTER_USE_KEY
);
if (empty($sheetData['data'])) {
return [];
}
$flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
$flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
$sheetDataXml = $flexFormTools->flexArray2Xml($sheetData);
return $flexFormService->convertFlexFormContentToArray($sheetDataXml)['settings'] ?? [];
}
}