‪TYPO3CMS  ‪main
CodeEditorElement.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 
28 
34 {
35  protected array ‪$resultArray = [];
36  protected string ‪$mode = '';
37 
43  protected ‪$defaultFieldInformation = [
44  'tcaDescription' => [
45  'renderType' => 'tcaDescription',
46  ],
47  ];
48 
54  protected ‪$defaultFieldWizard = [
55  'localizationStateSelector' => [
56  'renderType' => 'localizationStateSelector',
57  ],
58  'otherLanguageContent' => [
59  'renderType' => 'otherLanguageContent',
60  'after' => [
61  'localizationStateSelector',
62  ],
63  ],
64  'defaultLanguageDifferences' => [
65  'renderType' => 'defaultLanguageDifferences',
66  'after' => [
67  'otherLanguageContent',
68  ],
69  ],
70  ];
71 
80  public function ‪render(): array
81  {
82  $this->resultArray = $this->‪initializeResultArray();
83  $this->resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/code-editor/element/code-mirror-element.js');
84 
85  // Compile and register code editor configuration
86  GeneralUtility::makeInstance(CodeEditor::class)->registerConfiguration();
87 
88  $addonRegistry = GeneralUtility::makeInstance(AddonRegistry::class);
89  $registeredAddons = $addonRegistry->getAddons();
90  foreach ($registeredAddons as $addon) {
91  foreach ($addon->getCssFiles() as $cssFile) {
92  $this->resultArray['stylesheetFiles'][] = $cssFile;
93  }
94  }
95 
96  $parameterArray = $this->data['parameterArray'];
97 
98  $attributes = [
99  'wrap' => 'off',
100  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($parameterArray['fieldConf']['config']),
101  ];
102  if (isset($parameterArray['fieldConf']['config']['rows']) && ‪MathUtility::canBeInterpretedAsInteger($parameterArray['fieldConf']['config']['rows'])) {
103  $attributes['rows'] = $parameterArray['fieldConf']['config']['rows'];
104  }
105 
106  $settings = [];
107  if ($parameterArray['fieldConf']['config']['readOnly'] ?? false) {
108  $settings['readOnly'] = true;
109  }
110 
111  $editorHtml = $this->‪getHTMLCodeForEditor(
112  $parameterArray['itemFormElName'],
113  'form-control font-monospace enable-tab',
114  $parameterArray['itemFormElValue'],
115  $attributes,
116  $settings,
117  [
118  'target' => 0,
119  'effectivePid' => $this->data['effectivePid'] ?? 0,
120  ]
121  );
122 
123  $fieldInformationResult = $this->‪renderFieldInformation();
124  $fieldInformationHtml = $fieldInformationResult['html'];
125  $this->resultArray = $this->‪mergeChildReturnIntoExistingResult($this->resultArray, $fieldInformationResult, false);
126 
127  $fieldControlResult = $this->‪renderFieldControl();
128  $fieldControlHtml = $fieldControlResult['html'];
129  $this->resultArray = $this->‪mergeChildReturnIntoExistingResult($this->resultArray, $fieldControlResult, false);
130 
131  $fieldWizardResult = $this->‪renderFieldWizard();
132  $fieldWizardHtml = $fieldWizardResult['html'];
133  $this->resultArray = $this->‪mergeChildReturnIntoExistingResult($this->resultArray, $fieldWizardResult, false);
134 
135  $html = [];
136  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
137  $html[] = $fieldInformationHtml;
138  $html[] = '<div class="form-control-wrap">';
139  $html[] = '<div class="form-wizards-wrap">';
140  $html[] = '<div class="form-wizards-element">';
141  $html[] = $editorHtml;
142  $html[] = '</div>';
143  if (!empty($fieldControlHtml)) {
144  $html[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
145  $html[] = '<div class="btn-group">';
146  $html[] = $fieldControlHtml;
147  $html[] = '</div>';
148  $html[] = '</div>';
149  }
150  if (!empty($fieldWizardHtml)) {
151  $html[] = '<div class="form-wizards-items-bottom">';
152  $html[] = $fieldWizardHtml;
153  $html[] = '</div>';
154  }
155  $html[] = '</div>';
156  $html[] = '</div>';
157  $html[] = '</div>';
158 
159  $this->resultArray['html'] = $this->‪wrapWithFieldsetAndLegend(implode(LF, $html));
160 
161  return ‪$this->resultArray;
162  }
163 
175  protected function ‪getHTMLCodeForEditor(
176  string $name,
177  string $class = '',
178  string $content = '',
179  array $attributes = [],
180  array $settings = [],
181  array $hiddenfields = []
182  ): string {
183  $code = [];
184  ‪$mode = $this->‪getMode();
185  $addonRegistry = GeneralUtility::makeInstance(AddonRegistry::class);
186  $registeredAddons = $addonRegistry->getAddons();
187 
188  $attributes['class'] = $class;
189  $attributes['id'] = 't3editor_' . md5($name);
190  $attributes['name'] = $name;
191 
192  $settings = array_merge($addonRegistry->compileSettings($registeredAddons), $settings);
193 
194  $addons = [];
195  $keymaps = [];
196  foreach ($registeredAddons as $addon) {
197  $module = $addon->getModule();
198  $keymap = $addon->getKeymap();
199  if ($module) {
200  $addons[] = $module;
201  }
202  if ($keymap) {
203  $keymaps[] = $keymap;
204  }
205  }
206  $codeMirrorConfig = [
207  'name' => $name,
208  'mode' => GeneralUtility::jsonEncodeForHtmlAttribute(‪$mode->getModule(), false),
209  'addons' => GeneralUtility::jsonEncodeForHtmlAttribute($addons, false),
210  'keymaps' => GeneralUtility::jsonEncodeForHtmlAttribute($keymaps, false),
211  'options' => GeneralUtility::jsonEncodeForHtmlAttribute($settings, false),
212  ];
213 
214  $code[] = '<typo3-t3editor-codemirror ' . GeneralUtility::implodeAttributes($codeMirrorConfig, true) . '>';
215  $code[] = '<textarea ' . GeneralUtility::implodeAttributes($attributes, true) . '>' . htmlspecialchars($content) . '</textarea>';
216 
217  if (!empty($hiddenfields)) {
218  foreach ($hiddenfields as $attributeName => $value) {
219  $code[] = '<input type="hidden" name="' . htmlspecialchars((string)$attributeName) . '" value="' . htmlspecialchars((string)$value) . '" />';
220  }
221  }
222  $code[] = '</typo3-t3editor-codemirror>';
223 
224  return implode(LF, $code);
225  }
226 
230  protected function ‪getMode(): ‪Mode
231  {
232  $config = $this->data['parameterArray']['fieldConf']['config'];
233 
234  $registry = GeneralUtility::makeInstance(ModeRegistry::class);
235  if (!isset($config['format'])) {
236  return $registry->getDefaultMode();
237  }
238 
239  ‪$identifier = $config['format'];
240  if (str_contains($config['format'], '/')) {
241  $parts = explode('/', $config['format']);
242  ‪$identifier = end($parts);
243  }
244 
245  return $registry->getByFormatCode(‪$identifier);
246  }
247 }
‪TYPO3\CMS\Backend\CodeEditor\Registry\AddonRegistry
Definition: AddonRegistry.php:28
‪TYPO3\CMS\Backend\CodeEditor\Mode
Definition: Mode.php:27
‪TYPO3\CMS\Backend\Form\Element\CodeEditorElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: CodeEditorElement.php:52
‪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\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Backend\Form\Element\CodeEditorElement\getHTMLCodeForEditor
‪string getHTMLCodeForEditor(string $name, string $class='', string $content='', array $attributes=[], array $settings=[], array $hiddenfields=[])
Definition: CodeEditorElement.php:173
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element\CodeEditorElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: CodeEditorElement.php:42
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\CodeEditorElement
Definition: CodeEditorElement.php:34
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\wrapWithFieldsetAndLegend
‪wrapWithFieldsetAndLegend(string $innerHTML)
Definition: AbstractFormElement.php:133
‪TYPO3\CMS\Backend\Form\Element\CodeEditorElement\getMode
‪getMode()
Definition: CodeEditorElement.php:228
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:89
‪TYPO3\CMS\Backend\Form\Element\CodeEditorElement\$resultArray
‪array $resultArray
Definition: CodeEditorElement.php:35
‪TYPO3\CMS\Backend\Form\Element\CodeEditorElement\render
‪array render()
Definition: CodeEditorElement.php:78
‪TYPO3\CMS\Filelist\Type\Mode
‪Mode
Definition: Mode.php:24
‪TYPO3\CMS\Backend\Form\Element\CodeEditorElement\$mode
‪string $mode
Definition: CodeEditorElement.php:36
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:133
‪TYPO3\CMS\Backend\CodeEditor\Registry\ModeRegistry
Definition: ModeRegistry.php:29
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\CodeEditor\Exception\InvalidModeException
Definition: InvalidModeException.php:26
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:105
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77
‪TYPO3\CMS\Backend\CodeEditor\CodeEditor
Definition: CodeEditor.php:35