[!!!][TASK] Improve flex and TCA handling in FormEngine
[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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
20 use TYPO3\CMS\Lang\LanguageService;
21
22 /**
23 * The container handles single elements.
24 *
25 * This one is called by FlexFormTabsContainer, FlexFormNoTabsContainer or FlexFormContainerContainer.
26 * For single fields, the code is similar to SingleFieldContainer, processing will end up in single
27 * element classes depending on specific renderType of an element. Additionally, it determines if a
28 * section is handled and hands over to FlexFormSectionContainer in this case.
29 */
30 class FlexFormElementContainer extends AbstractContainer
31 {
32 /**
33 * Entry method
34 *
35 * @return array As defined in initializeResultArray() of AbstractNode
36 */
37 public function render()
38 {
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 $options = $this->data;
65 $options['flexFormDataStructureArray'] = $flexFormFieldArray;
66 $options['flexFormRowData'] = isset($flexFormRowData[$flexFormFieldName]['el']) ? $flexFormRowData[$flexFormFieldName]['el'] : [];
67 $options['flexFormFieldName'] = $flexFormFieldName;
68 $options['renderType'] = 'flexFormSectionContainer';
69 $sectionContainerResult = $this->nodeFactory->create($options)->render();
70 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $sectionContainerResult);
71 } else {
72 // Set up options for single element
73 $fakeParameterArray = [
74 'fieldConf' => [
75 'label' => $languageService->sL(trim($flexFormFieldArray['label'])),
76 'config' => $flexFormFieldArray['config'],
77 'children' => $flexFormFieldArray['children'],
78 'defaultExtras' => $flexFormFieldArray['defaultExtras'],
79 'onChange' => $flexFormFieldArray['onChange'],
80 ],
81 'fieldChangeFunc' => $parameterArray['fieldChangeFunc'],
82 'label' => $parameterArray['label'],
83 ];
84
85 $alertMsgOnChange = '';
86 if (isset($fakeParameterArray['fieldConf']['onChange']) && $fakeParameterArray['fieldConf']['onChange'] === 'reload') {
87 if ($this->getBackendUserAuthentication()->jsConfirmation(JsConfirmation::TYPE_CHANGE)) {
88 $alertMsgOnChange = 'top.TYPO3.Modal.confirm('
89 . 'TYPO3.lang["FormEngine.refreshRequiredTitle"],'
90 . ' TYPO3.lang["FormEngine.refreshRequiredContent"]'
91 . ')'
92 . '.on('
93 . '"button.clicked",'
94 . ' function(e) { if (e.target.name == "ok" && TBE_EDITOR.checkSubmit(-1)) { TBE_EDITOR.submitForm() } top.TYPO3.Modal.dismiss(); }'
95 . ');';
96 } else {
97 $alertMsgOnChange = 'if (TBE_EDITOR.checkSubmit(-1)){ TBE_EDITOR.submitForm();}';
98 }
99 }
100 if ($alertMsgOnChange) {
101 $fakeParameterArray['fieldChangeFunc']['alert'] = $alertMsgOnChange;
102 }
103
104 $originalFieldName = $parameterArray['itemFormElName'];
105 $fakeParameterArray['itemFormElName'] = $parameterArray['itemFormElName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
106 if ($fakeParameterArray['itemFormElName'] !== $originalFieldName) {
107 // If calculated itemFormElName is different from originalFieldName
108 // change the originalFieldName in TBE_EDITOR_fieldChanged. This is
109 // especially relevant for wizards writing their content back to hidden fields
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 // @todo: is that a bug? name and id should usually be of different form
115 $fakeParameterArray['itemFormElID'] = $fakeParameterArray['itemFormElName'];
116 if (isset($flexFormRowData[$flexFormFieldName]['vDEF'])) {
117 $fakeParameterArray['itemFormElValue'] = $flexFormRowData[$flexFormFieldName]['vDEF'];
118 } else {
119 $fakeParameterArray['itemFormElValue'] = $fakeParameterArray['fieldConf']['config']['default'];
120 }
121
122 $options = $this->data;
123 // Set either flexFormFieldName or flexFormContainerFieldName, depending on if we are a "regular" field or a flex container section field
124 if (empty($options['flexFormFieldName'])) {
125 $options['flexFormFieldName'] = $flexFormFieldName;
126 } else {
127 $options['flexFormContainerFieldName'] = $flexFormFieldName;
128 }
129 $options['parameterArray'] = $fakeParameterArray;
130 $options['elementBaseName'] = $this->data['elementBaseName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
131
132 if (!empty($flexFormFieldArray['config']['renderType'])) {
133 $options['renderType'] = $flexFormFieldArray['config']['renderType'];
134 } else {
135 // Fallback to type if no renderType is given
136 $options['renderType'] = $flexFormFieldArray['config']['type'];
137 }
138 $childResult = $this->nodeFactory->create($options)->render();
139
140 // Possible line breaks in the label through xml: \n => <br/>, usage of nl2br() not possible, so it's done through str_replace (?!)
141 $processedTitle = str_replace('\\n', '<br />', htmlspecialchars($fakeParameterArray['fieldConf']['label']));
142 $html = [];
143 $html[] = '<div class="form-section">';
144 $html[] = '<div class="form-group t3js-formengine-palette-field t3js-formengine-validation-marker">';
145 $html[] = '<label class="t3js-formengine-label">';
146 $html[] = BackendUtility::wrapInHelp($parameterArray['_cshKey'], $flexFormFieldName, $processedTitle);
147 $html[] = '</label>';
148 $html[] = '<div class="t3js-formengine-field-item">';
149 $html[] = $childResult['html'];
150 $html[] = '</div>';
151 $html[] = '</div>';
152 $html[] = '</div>';
153
154 $resultArray['html'] .= implode(LF, $html);
155 $childResult['html'] = '';
156 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childResult);
157 }
158 }
159
160 return $resultArray;
161 }
162
163 /**
164 * @return LanguageService
165 */
166 protected function getLanguageService()
167 {
168 return $GLOBALS['LANG'];
169 }
170
171 /**
172 * @return BackendUserAuthentication
173 */
174 protected function getBackendUserAuthentication()
175 {
176 return $GLOBALS['BE_USER'];
177 }
178 }