‪TYPO3CMS  10.4
FlexFormElementContainer.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
32 {
38  public function ‪render()
39  {
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  $showFieldName = ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['debug'] && $this->‪getBackendUserAuthentication()->‪isAdmin();
48 
49  foreach ($flexFormDataStructureArray as $flexFormFieldName => $flexFormFieldArray) {
50  if (
51  // No item array found at all
52  !is_array($flexFormFieldArray)
53  // Not a section or container and not a list of single items
54  || (!isset($flexFormFieldArray['type']) && !is_array($flexFormFieldArray['config']))
55  ) {
56  continue;
57  }
58 
59  if ($flexFormFieldArray['type'] === 'array') {
60  // Section
61  if (empty($flexFormFieldArray['section'])) {
62  $resultArray['html'] = LF . 'Section expected at ' . $flexFormFieldName . ' but not found';
63  continue;
64  }
65 
66  $options = ‪$this->data;
67  $options['flexFormDataStructureArray'] = $flexFormFieldArray;
68  $options['flexFormRowData'] = $flexFormRowData[$flexFormFieldName]['el'] ?? [];
69  $options['flexFormFieldName'] = $flexFormFieldName;
70  $options['renderType'] = 'flexFormSectionContainer';
71  $sectionContainerResult = $this->nodeFactory->create($options)->render();
72  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $sectionContainerResult);
73  } else {
74  // Set up options for single element
75  $fakeParameterArray = [
76  'fieldConf' => [
77  'label' => $languageService->sL(trim($flexFormFieldArray['label'])),
78  'config' => $flexFormFieldArray['config'],
79  'children' => $flexFormFieldArray['children'],
80  'onChange' => $flexFormFieldArray['onChange'],
81  ],
82  'fieldChangeFunc' => $parameterArray['fieldChangeFunc'],
83  'label' => $parameterArray['label'],
84  ];
85 
86  if (isset($flexFormFieldArray['description']) && !empty($flexFormFieldArray['description'])) {
87  $fakeParameterArray['fieldConf']['description'] = $flexFormFieldArray['description'];
88  }
89 
90  $alertMsgOnChange = '';
91  if (isset($fakeParameterArray['fieldConf']['onChange']) && $fakeParameterArray['fieldConf']['onChange'] === 'reload') {
93  $alertMsgOnChange = 'Modal.confirm('
94  . 'TYPO3.lang["FormEngine.refreshRequiredTitle"],'
95  . ' TYPO3.lang["FormEngine.refreshRequiredContent"]'
96  . ')'
97  . '.on('
98  . '"button.clicked",'
99  . ' function(e) { if (e.target.name == "ok") { FormEngine.saveDocument(); } Modal.dismiss(); }'
100  . ');';
101  } else {
102  $alertMsgOnChange = 'FormEngine.saveDocument();';
103  }
104  }
105  if ($alertMsgOnChange) {
106  $fakeParameterArray['fieldChangeFunc']['alert'] = 'require([\'TYPO3/CMS/Backend/FormEngine\', \'TYPO3/CMS/Backend/Modal\'], function (FormEngine, Modal) {' . $alertMsgOnChange . '});';
107  }
108 
109  $originalFieldName = $parameterArray['itemFormElName'];
110  $fakeParameterArray['itemFormElName'] = $parameterArray['itemFormElName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
111  if ($fakeParameterArray['itemFormElName'] !== $originalFieldName) {
112  // If calculated itemFormElName is different from originalFieldName
113  // change the originalFieldName in TBE_EDITOR_fieldChanged. This is
114  // especially relevant for wizards writing their content back to hidden fields
115  if (!empty($fakeParameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'])) {
116  $fakeParameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] = str_replace($originalFieldName, $fakeParameterArray['itemFormElName'], $fakeParameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged']);
117  }
118  }
119  $fakeParameterArray['itemFormElID'] = $parameterArray['itemFormElID'] . '_' . preg_replace('/[^a-zA-Z0-9_-]/', '_', $flexFormFieldName) . '_' . md5($fakeParameterArray['itemFormElName']);
120  if (isset($flexFormRowData[$flexFormFieldName]['vDEF'])) {
121  $fakeParameterArray['itemFormElValue'] = $flexFormRowData[$flexFormFieldName]['vDEF'];
122  } else {
123  $fakeParameterArray['itemFormElValue'] = $fakeParameterArray['fieldConf']['config']['default'];
124  }
125 
126  $options = ‪$this->data;
127  // Set either flexFormFieldName or flexFormContainerFieldName, depending on if we are a "regular" field or a flex container section field
128  if (empty($options['flexFormFieldName'])) {
129  $options['flexFormFieldName'] = $flexFormFieldName;
130  } else {
131  $options['flexFormContainerFieldName'] = $flexFormFieldName;
132  }
133  $options['parameterArray'] = $fakeParameterArray;
134  $options['elementBaseName'] = $this->data['elementBaseName'] . $flexFormFormPrefix . '[' . $flexFormFieldName . '][vDEF]';
135 
136  if (!empty($flexFormFieldArray['config']['renderType'])) {
137  $options['renderType'] = $flexFormFieldArray['config']['renderType'];
138  } else {
139  // Fallback to type if no renderType is given
140  $options['renderType'] = $flexFormFieldArray['config']['type'];
141  }
142  $childResult = $this->nodeFactory->create($options)->render();
143 
144  if (!empty($childResult['html'])) {
145  // Possible line breaks in the label through xml: \n => <br/>, usage of nl2br() not possible, so it's done through str_replace (?!)
146  $processedTitle = str_replace('\\n', '<br />', htmlspecialchars($fakeParameterArray['fieldConf']['label']));
147  $html = [];
148  $html[] = '<div class="form-section">';
149  $html[] = '<div class="form-group t3js-formengine-palette-field t3js-formengine-validation-marker">';
150  $html[] = '<label class="t3js-formengine-label">';
151  $html[] = ‪BackendUtility::wrapInHelp($parameterArray['_cshKey'], $flexFormFieldName, $processedTitle);
152  $html[] = $showFieldName ? ('<code>[' . htmlspecialchars($flexFormFieldName) . ']</code>') : '';
153  $html[] = '</label>';
154  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
155  $html[] = $childResult['html'];
156  $html[] = '</div>';
157  $html[] = '</div>';
158  $html[] = '</div>';
159  $resultArray['html'] .= implode(LF, $html);
160  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $childResult, false);
161  }
162  }
163  }
164 
165  return $resultArray;
166  }
167 
171  protected function ‪getLanguageService()
172  {
173  return ‪$GLOBALS['LANG'];
174  }
175 
179  protected function ‪getBackendUserAuthentication()
180  {
181  return ‪$GLOBALS['BE_USER'];
182  }
183 }
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:116
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:90
‪TYPO3\CMS\Backend\Form\Container\FlexFormElementContainer\render
‪array render()
Definition: FlexFormElementContainer.php:38
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\isAdmin
‪bool isAdmin()
Definition: BackendUserAuthentication.php:292
‪TYPO3\CMS\Backend\Form\Container
Definition: AbstractContainer.php:16
‪TYPO3\CMS\Backend\Utility\BackendUtility\wrapInHelp
‪static string wrapInHelp($table, $field, $text='', array $overloadHelpText=[])
Definition: BackendUtility.php:2260
‪TYPO3\CMS\Backend\Form\AbstractNode\$data
‪array $data
Definition: AbstractNode.php:42
‪TYPO3\CMS\Backend\Form\Container\AbstractContainer
Definition: AbstractContainer.php:28
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Form\Container\FlexFormElementContainer\getBackendUserAuthentication
‪BackendUserAuthentication getBackendUserAuthentication()
Definition: FlexFormElementContainer.php:179
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Type\Bitmask\JsConfirmation\TYPE_CHANGE
‪const TYPE_CHANGE
Definition: JsConfirmation.php:29
‪TYPO3\CMS\Backend\Form\Container\FlexFormElementContainer
Definition: FlexFormElementContainer.php:32
‪TYPO3\CMS\Core\Type\Bitmask\JsConfirmation
Definition: JsConfirmation.php:25
‪TYPO3\CMS\Backend\Form\Container\FlexFormElementContainer\getLanguageService
‪LanguageService getLanguageService()
Definition: FlexFormElementContainer.php:171