‪TYPO3CMS  ‪main
JsonElement.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
27 
34 {
40  protected ‪$defaultFieldInformation = [
41  'tcaDescription' => [
42  'renderType' => 'tcaDescription',
43  ],
44  ];
45 
51  protected ‪$defaultFieldWizard = [
52  'localizationStateSelector' => [
53  'renderType' => 'localizationStateSelector',
54  ],
55  'otherLanguageContent' => [
56  'renderType' => 'otherLanguageContent',
57  'after' => [
58  'localizationStateSelector',
59  ],
60  ],
61  'defaultLanguageDifferences' => [
62  'renderType' => 'defaultLanguageDifferences',
63  'after' => [
64  'otherLanguageContent',
65  ],
66  ],
67  ];
68 
69  public function ‪render(): array
70  {
71  $resultArray = $this->‪initializeResultArray();
72 
73  $parameterArray = $this->data['parameterArray'];
74  $config = $parameterArray['fieldConf']['config'];
75  $readOnly = (bool)($config['readOnly'] ?? false);
76  $placeholder = trim((string)($config['placeholder'] ?? ''));
77  $enableCodeEditor = $config['enableCodeEditor'] ?? true;
78 
79  $itemValue = '';
80  if (!empty($parameterArray['itemFormElValue'])) {
81  try {
82  $itemValue = (string)json_encode($parameterArray['itemFormElValue'], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
83  } catch (\JsonException) {
84  }
85  }
86 
87  $width = null;
88  if ($config['cols'] ?? false) {
89  $width = $this->‪formMaxWidth(‪MathUtility::forceIntegerInRange($config['cols'], $this->minimumInputWidth, $this->maxInputWidth));
90  }
91 
92  $rows = ‪MathUtility::forceIntegerInRange(($config['rows'] ?? 5) ?: 5, 1, 20);
93  $originalRows = $rows;
94  if (($itemFormElementValueLength = strlen($itemValue)) > 80) {
95  $calculatedRows = ‪MathUtility::forceIntegerInRange(
96  (int)round($itemFormElementValueLength / 40),
97  count(explode(LF, $itemValue)),
98  20
99  );
100  if ($originalRows < $calculatedRows) {
101  $rows = $calculatedRows;
102  }
103  }
104  $fieldId = ‪StringUtility::getUniqueId('formengine-json-');
105 
106  $fieldInformationResult = $this->‪renderFieldInformation();
107  $fieldInformationHtml = $fieldInformationResult['html'];
108  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
109 
110  if ($readOnly && !$enableCodeEditor) {
111  $html = [];
112  $html[] = $this->‪renderLabel($fieldId);
113  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
114  $html[] = $fieldInformationHtml;
115  $html[] = '<div class="form-wizards-wrap">';
116  $html[] = '<div class="form-wizards-element">';
117  $html[] = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px">' : '>');
118  $html[] = '<textarea class="form-control font-monospace" id="' . htmlspecialchars($fieldId) . '" rows="' . $rows . '" disabled>';
119  $html[] = htmlspecialchars($itemValue);
120  $html[] = '</textarea>';
121  $html[] = '</div>';
122  $html[] = '</div>';
123  $html[] = '</div>';
124  $html[] = '</div>';
125  $resultArray['html'] = implode(LF, $html);
126  return $resultArray;
127  }
128 
129  $itemName = (string)$parameterArray['itemFormElName'];
130  $attributes = [
131  'id' => $fieldId,
132  'name' => $itemName,
133  'wrap' => 'off',
134  'rows' => (string)$rows,
135  'class' => 'form-control font-monospace',
136  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
137  ];
138 
139  if ($readOnly) {
140  $attributes['disabled'] = '';
141  }
142 
143  if ($placeholder !== '') {
144  $attributes['placeholder'] = $placeholder;
145  }
146 
147  // Use CodeMirror if available
148  if ($enableCodeEditor) {
149  // Compile and register code editor configuration
150  GeneralUtility::makeInstance(CodeEditor::class)->registerConfiguration();
151 
152  $modeRegistry = GeneralUtility::makeInstance(ModeRegistry::class);
153  $mode = $modeRegistry->isRegistered('json')
154  ? $modeRegistry->getByFormatCode('json')
155  : $modeRegistry->getDefaultMode();
156 
157  $addons = $keymaps = [];
158  foreach (GeneralUtility::makeInstance(AddonRegistry::class)->getAddons() as $addon) {
159  foreach ($addon->getCssFiles() as $cssFile) {
160  $resultArray['stylesheetFiles'][] = $cssFile;
161  }
162  if (($module = $addon->getModule())) {
163  $addons[] = $module;
164  }
165  if (($keymap = $addon->getKeymap())) {
166  $keymaps[] = $keymap;
167  }
168  }
169 
170  $codeMirrorConfig = [
171  'mode' => GeneralUtility::jsonEncodeForHtmlAttribute($mode->getModule(), false),
172  ];
173 
174  if ($readOnly) {
175  $codeMirrorConfig['readonly'] = '';
176  }
177  if ($placeholder !== '') {
178  $codeMirrorConfig['placeholder'] = $placeholder;
179  }
180  if ($addons !== []) {
181  $codeMirrorConfig['addons'] = GeneralUtility::jsonEncodeForHtmlAttribute($addons, false);
182  }
183  if ($keymaps !== []) {
184  $codeMirrorConfig['keymaps'] = GeneralUtility::jsonEncodeForHtmlAttribute($keymaps, false);
185  }
186 
187  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/code-editor/element/code-mirror-element.js');
188  $editorHtml = '
189  <typo3-t3editor-codemirror ' . GeneralUtility::implodeAttributes($codeMirrorConfig, true, true) . '>
190  <textarea ' . GeneralUtility::implodeAttributes($attributes, true, true) . '>' . htmlspecialchars($itemValue) . '</textarea>
191  <input type="hidden" name="target" value="0" />
192  <input type="hidden" name="effectivePid" value="' . htmlspecialchars((string)($this->data['effectivePid'] ?? '0')) . '" />
193  </typo3-t3editor-codemirror>';
194  } else {
195  $attributes['class'] = implode(' ', array_merge(explode(' ', $attributes['class']), ['formengine-textarea', 't3js-enable-tab']));
196  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/form-engine/element/json-element.js');
197  $editorHtml = '
198  <typo3-formengine-element-json recordFieldId="' . htmlspecialchars($fieldId) . '">
199  <textarea ' . GeneralUtility::implodeAttributes($attributes, true, true) . '>' . htmlspecialchars($itemValue) . '</textarea>
200  </typo3-formengine-element-json>';
201  }
202 
203  $additionalHtml = [];
204  if (!$readOnly) {
205  $fieldControlResult = $this->‪renderFieldControl();
206  $fieldControlHtml = $fieldControlResult['html'];
207  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
208 
209  if (!empty($fieldControlHtml)) {
210  $additionalHtml[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
211  $additionalHtml[] = '<div class="btn-group">';
212  $additionalHtml[] = $fieldControlHtml;
213  $additionalHtml[] = '</div>';
214  $additionalHtml[] = '</div>';
215  }
216 
217  $fieldWizardResult = $this->‪renderFieldWizard();
218  $fieldWizardHtml = $fieldWizardResult['html'];
219  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
220 
221  if (!empty($fieldWizardHtml)) {
222  $additionalHtml[] = '<div class="form-wizards-items-bottom">';
223  $additionalHtml[] = $fieldWizardHtml;
224  $additionalHtml[] = '</div>';
225  }
226  }
227 
228  $html = [];
229  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
230  $html[] = $fieldInformationHtml;
231  $html[] = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px">' : '>');
232  $html[] = '<div class="form-wizards-wrap">';
233  $html[] = '<div class="form-wizards-element">';
234  $html[] = $editorHtml;
235  $html[] = '</div>';
236  $html[] = implode(LF, $additionalHtml);
237  $html[] = '</div>';
238  $html[] = '</div>';
239  $html[] = '</div>';
240 
241  $resultArray['html'] = $this->‪wrapWithFieldsetAndLegend(implode(LF, $html));
242 
243  return $resultArray;
244  }
245 }
‪TYPO3\CMS\Backend\CodeEditor\Registry\AddonRegistry
Definition: AddonRegistry.php:28
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Backend\Form\Element\JsonElement\render
‪render()
Definition: JsonElement.php:67
‪TYPO3\CMS\Backend\Form\Element\JsonElement
Definition: JsonElement.php:34
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\wrapWithFieldsetAndLegend
‪wrapWithFieldsetAndLegend(string $innerHTML)
Definition: AbstractFormElement.php:133
‪TYPO3\CMS\Backend\Form\Element\JsonElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: JsonElement.php:39
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:89
‪TYPO3\CMS\Backend\Form\Element\JsonElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: JsonElement.php:49
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:332
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:133
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderLabel
‪renderLabel(string $for)
Definition: AbstractFormElement.php:119
‪TYPO3\CMS\Backend\CodeEditor\Registry\ModeRegistry
Definition: ModeRegistry.php:29
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, ?string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:105
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77
‪TYPO3\CMS\Backend\CodeEditor\CodeEditor
Definition: CodeEditor.php:35