[BUGFIX] "form field wizard" in flexform not saved
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Container / FlexFormElementContainer.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Container;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Lang\LanguageService;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
22
23 /**
24 * The container handles single elements.
25 *
26 * This one is called by FlexFormTabsContainer, FlexFormNoTabsContainer or FlexFormContainerContainer.
27 * For single fields, the code is similar to SingleFieldContainer, processing will end up in single
28 * element classes depending on specific type of an element. Additionally, it determines if a
29 * section is handled and hands over to FlexFormSectionContainer in this case.
30 */
31 class FlexFormElementContainer extends AbstractContainer {
32
33 /**
34 * Entry method
35 *
36 * @return array As defined in initializeResultArray() of AbstractNode
37 */
38 public function render() {
39 $table = $this->data['tableName'];
40 $flexFormDataStructureArray = $this->data['flexFormDataStructureArray'];
41 $flexFormRowData = $this->data['flexFormRowData'];
42 $flexFormFormPrefix = $this->data['flexFormFormPrefix'];
43 $parameterArray = $this->data['parameterArray'];
44
45 $languageService = $this->getLanguageService();
46 $resultArray = $this->initializeResultArray();
47 foreach ($flexFormDataStructureArray as $flexFormFieldName => $flexFormFieldArray) {
48 if (
49 // No item array found at all
50 !is_array($flexFormFieldArray)
51 // Not a section or container and not a list of single items
52 || (!isset($flexFormFieldArray['type']) && !is_array($flexFormFieldArray['config']))
53 ) {
54 continue;
55 }
56
57 if ($flexFormFieldArray['type'] === 'array') {
58 // Section
59 if (empty($flexFormFieldArray['section'])) {
60 $resultArray['html'] = LF . 'Section expected at ' . $flexFormFieldName . ' but not found';
61 continue;
62 }
63
64 $sectionTitle = '';
65 if (!empty($flexFormFieldArray['title'])) {
66 $sectionTitle = $languageService->sL($flexFormFieldArray['title']);
67 }
68
69 $options = $this->data;
70 $options['flexFormDataStructureArray'] = $flexFormFieldArray['el'];
71 $options['flexFormRowData'] = isset($flexFormRowData[$flexFormFieldName]['el']) ? $flexFormRowData[$flexFormFieldName]['el'] : [];
72 $options['flexFormSectionType'] = $flexFormFieldName;
73 $options['flexFormSectionTitle'] = $sectionTitle;
74 $options['renderType'] = 'flexFormSectionContainer';
75 $sectionContainerResult = $this->nodeFactory->create($options)->render();
76 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $sectionContainerResult);
77 } else {
78 // Set up options for single element
79 $fakeParameterArray = [
80 'fieldConf' => [
81 'label' => $languageService->sL(trim($flexFormFieldArray['label'])),
82 'config' => $flexFormFieldArray['config'],
83 'defaultExtras' => $flexFormFieldArray['defaultExtras'],
84 'onChange' => $flexFormFieldArray['onChange'],
85 ],
86 'fieldChangeFunc' => $parameterArray['fieldChangeFunc'],
87 'onFocus' => $parameterArray['onFocus'],
88 'label' => $parameterArray['label'],
89 ];
90
91 $alertMsgOnChange = '';
92 if (
93 $fakeParameterArray['fieldConf']['onChange'] === 'reload'
94 || !empty($GLOBALS['TCA'][$table]['ctrl']['type']) && $GLOBALS['TCA'][$table]['ctrl']['type'] === $flexFormFieldName
95 || !empty($GLOBALS['TCA'][$table]['ctrl']['requestUpdate']) && GeneralUtility::inList($GLOBALS['TCA'][$table]['ctrl']['requestUpdate'], $flexFormFieldName)
96 ) {
97 if ($this->getBackendUserAuthentication()->jsConfirmation(JsConfirmation::TYPE_CHANGE)) {
98 $alertMsgOnChange = 'top.TYPO3.Modal.confirm(TBE_EDITOR.labels.refreshRequired.title, TBE_EDITOR.labels.refreshRequired.content).on("button.clicked", function(e) { if (e.target.name == "ok" && TBE_EDITOR.checkSubmit(-1)) { TBE_EDITOR.submitForm() } top.TYPO3.Modal.dismiss(); });';
99 } else {
100 $alertMsgOnChange = 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm();}';
101 }
102 }
103 if ($alertMsgOnChange) {
104 $fakeParameterArray['fieldChangeFunc']['alert'] = $alertMsgOnChange;
105 }
106
107 $originalFieldName = $parameterArray['itemFormElName'];
108 $fakeParameterArray['itemFormElName'] = $parameterArray['itemFormElName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
109 if ($fakeParameterArray['itemFormElName'] !== $originalFieldName) {
110 if (!empty($fakeParameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'])) {
111 $fakeParameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] = str_replace($originalFieldName, $fakeParameterArray['itemFormElName'], $fakeParameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged']);
112 }
113 }
114 $fakeParameterArray['itemFormElID'] = $fakeParameterArray['itemFormElName'];
115 if (isset($flexFormRowData[$flexFormFieldName]['vDEF'])) {
116 $fakeParameterArray['itemFormElValue'] = $flexFormRowData[$flexFormFieldName]['vDEF'];
117 } else {
118 $fakeParameterArray['itemFormElValue'] = $fakeParameterArray['fieldConf']['config']['default'];
119 }
120
121 $options = $this->data;
122 $options['parameterArray'] = $fakeParameterArray;
123 $options['elementBaseName'] = $this->data['elementBaseName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
124
125 if (!empty($flexFormFieldArray['config']['renderType'])) {
126 $options['renderType'] = $flexFormFieldArray['config']['renderType'];
127 } else {
128 // Fallback to type if no renderType is given
129 $options['renderType'] = $flexFormFieldArray['config']['type'];
130 }
131 $childResult = $this->nodeFactory->create($options)->render();
132
133 // Possible line breaks in the label through xml: \n => <br/>, usage of nl2br() not possible, so it's done through str_replace (?!)
134 $processedTitle = str_replace('\\n', '<br />', htmlspecialchars($fakeParameterArray['fieldConf']['label']));
135 $html = [];
136 $html[] = '<div class="form-section">';
137 $html[] = '<div class="form-group t3js-formengine-palette-field t3js-formengine-validation-marker">';
138 $html[] = '<label class="t3js-formengine-label">';
139 $html[] = BackendUtility::wrapInHelp($parameterArray['_cshKey'], $flexFormFieldName, $processedTitle);
140 $html[] = '</label>';
141 $html[] = '<div class="t3js-formengine-field-item">';
142 $html[] = $childResult['html'];
143 $html[] = '</div>';
144 $html[] = '</div>';
145 $html[] = '</div>';
146
147 $resultArray['html'] .= implode(LF, $html);
148 $childResult['html'] = '';
149 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childResult);
150 }
151 }
152
153 return $resultArray;
154 }
155
156 /**
157 * @return LanguageService
158 */
159 protected function getLanguageService() {
160 return $GLOBALS['LANG'];
161 }
162
163 /**
164 * @return BackendUserAuthentication
165 */
166 protected function getBackendUserAuthentication() {
167 return $GLOBALS['BE_USER'];
168 }
169
170 }