[!!!][TASK] Drop evaluation of @inject annotations
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Controller / FormFrontendController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Form\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
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 use TYPO3\CMS\Core\Utility\ArrayUtility;
19 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
20 use TYPO3\CMS\Form\Domain\Configuration\ConfigurationService;
21 use TYPO3\CMS\Form\Mvc\Configuration\TypoScriptService;
22
23 /**
24 * The frontend controller
25 *
26 * Scope: frontend
27 * @internal
28 */
29 class FormFrontendController extends ActionController
30 {
31
32 /**
33 * @var \TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface
34 */
35 protected $formPersistenceManager;
36
37 /**
38 * @param \TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface $formPersistenceManager
39 * @internal
40 */
41 public function injectFormPersistenceManager(\TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface $formPersistenceManager)
42 {
43 $this->formPersistenceManager = $formPersistenceManager;
44 }
45
46 /**
47 * Take the form which should be rendered from the plugin settings
48 * and overlay the formDefinition with additional data from
49 * flexform and typoscript settings.
50 * This method is used directly to display the first page from the
51 * formDefinition because its cached.
52 *
53 * @internal
54 */
55 public function renderAction()
56 {
57 $formDefinition = [];
58 if (!empty($this->settings['persistenceIdentifier'])) {
59 $formDefinition = $this->formPersistenceManager->load($this->settings['persistenceIdentifier']);
60 $formDefinition['persistenceIdentifier'] = $this->settings['persistenceIdentifier'];
61 $formDefinition = $this->overrideByTypoScriptSettings($formDefinition);
62 $formDefinition = $this->overrideByFlexFormSettings($formDefinition);
63 $formDefinition = ArrayUtility::setValueByPath($formDefinition, 'renderingOptions._originalIdentifier', $formDefinition['identifier'], '.');
64 $formDefinition['identifier'] .= '-' . $this->configurationManager->getContentObject()->data['uid'];
65 }
66 $this->view->assign('formConfiguration', $formDefinition);
67 }
68
69 /**
70 * This method is used to display all pages / finishers except the
71 * first page because its non cached.
72 *
73 * @internal
74 */
75 public function performAction()
76 {
77 $this->forward('render');
78 }
79
80 /**
81 * Override the formDefinition with additional data from the Flexform
82 * settings. For now, only finisher settings are overridable.
83 *
84 * @param array $formDefinition
85 * @return array
86 */
87 protected function overrideByFlexFormSettings(array $formDefinition): array
88 {
89 if (isset($formDefinition['finishers'])) {
90 foreach ($formDefinition['finishers'] as &$finisherValue) {
91 $finisherIdentifier = $finisherValue['identifier'];
92 if ($this->settings['overrideFinishers'] && isset($this->settings['finishers'][$finisherIdentifier])) {
93 $prototypeName = $formDefinition['prototypeName'] ?? 'standard';
94 $configurationService = $this->objectManager->get(ConfigurationService::class);
95 $prototypeConfiguration = $configurationService->getPrototypeConfiguration($prototypeName);
96
97 foreach ($finisherValue['options'] as $optionKey => $optionValue) {
98 // If a previous overridden finisher property is excluded at some time
99 // it is still present in the flexform database row.
100 // To avoid a override from the time the property is excluded, this check is needed
101 if (!isset($prototypeConfiguration['finishersDefinition'][$finisherIdentifier]['FormEngine']['elements'][$optionKey])) {
102 continue;
103 }
104 if (isset($this->settings['finishers'][$finisherIdentifier][$optionKey])) {
105 $finisherValue['options'][$optionKey] = $this->settings['finishers'][$finisherIdentifier][$optionKey];
106 }
107 }
108 }
109 }
110 }
111 return $formDefinition;
112 }
113
114 /**
115 * Every formDefinition setting are overridable by typoscript.
116 * If the typoscript configuration path
117 * plugin.tx_form.settings.formDefinitionOverrides.<identifier>
118 * exists, this settings are merged into the formDefinition.
119 *
120 * @param array $formDefinition
121 * @return array
122 */
123 protected function overrideByTypoScriptSettings(array $formDefinition): array
124 {
125 if (
126 isset($this->settings['formDefinitionOverrides'][$formDefinition['identifier']])
127 && !empty($this->settings['formDefinitionOverrides'][$formDefinition['identifier']])
128 ) {
129 ArrayUtility::mergeRecursiveWithOverrule(
130 $formDefinition,
131 $this->settings['formDefinitionOverrides'][$formDefinition['identifier']]
132 );
133 $formDefinition = $this->objectManager->get(TypoScriptService::class)
134 ->resolvePossibleTypoScriptConfiguration($formDefinition);
135 }
136 return $formDefinition;
137 }
138 }