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