‪TYPO3CMS  10.4
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 
28 
34 {
38  protected ‪$resultArray;
39 
43  protected ‪$mode = '';
44 
50  protected ‪$defaultFieldInformation = [
51  'tcaDescription' => [
52  'renderType' => 'tcaDescription',
53  ],
54  ];
55 
61  protected ‪$defaultFieldWizard = [
62  'localizationStateSelector' => [
63  'renderType' => 'localizationStateSelector',
64  ],
65  'otherLanguageContent' => [
66  'renderType' => 'otherLanguageContent',
67  'after' => [
68  'localizationStateSelector',
69  ],
70  ],
71  'defaultLanguageDifferences' => [
72  'renderType' => 'defaultLanguageDifferences',
73  'after' => [
74  'otherLanguageContent',
75  ],
76  ],
77  ];
78 
87  public function ‪render(): array
88  {
89  $this->resultArray = $this->‪initializeResultArray();
90  $this->resultArray['stylesheetFiles'][] = 'EXT:t3editor/Resources/Public/JavaScript/Contrib/cm/lib/codemirror.css';
91  $this->resultArray['stylesheetFiles'][] = 'EXT:t3editor/Resources/Public/Css/t3editor.css';
92  $this->resultArray['requireJsModules'][] = [
93  'TYPO3/CMS/T3editor/T3editor' => 'function(T3editor) {T3editor.observeEditorCandidates()}'
94  ];
95 
96  // Compile and register t3editor configuration
97  GeneralUtility::makeInstance(T3editor::class)->registerConfiguration();
98 
99  $registeredAddons = ‪AddonRegistry::getInstance()->getForMode($this->‪getMode()->getFormatCode());
100  foreach ($registeredAddons as $addon) {
101  foreach ($addon->getCssFiles() as $cssFile) {
102  $this->resultArray['stylesheetFiles'][] = $cssFile;
103  }
104  }
105 
106  $parameterArray = $this->data['parameterArray'];
107 
108  $attributes = [];
109  if (isset($parameterArray['fieldConf']['config']['rows']) && ‪MathUtility::canBeInterpretedAsInteger($parameterArray['fieldConf']['config']['rows'])) {
110  $attributes['rows'] = $parameterArray['fieldConf']['config']['rows'];
111  }
112 
113  $attributes['wrap'] = 'off';
114  $attributes['style'] = 'width:100%;';
115  $attributes['onchange'] = GeneralUtility::quoteJSvalue($parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged']);
116 
117  $attributeString = GeneralUtility::implodeAttributes($attributes, true);
118  $editorHtml = $this->‪getHTMLCodeForEditor(
119  $parameterArray['itemFormElName'],
120  'text-monospace enable-tab',
121  $parameterArray['itemFormElValue'],
122  $attributeString,
123  $this->data['tableName'] . ' > ' . $this->data['fieldName'],
124  [
125  'target' => 0,
126  'effectivePid' => $this->data['effectivePid']
127  ]
128  );
129 
130  $fieldInformationResult = $this->‪renderFieldInformation();
131  $fieldInformationHtml = $fieldInformationResult['html'];
132  $this->resultArray = $this->‪mergeChildReturnIntoExistingResult($this->resultArray, $fieldInformationResult, false);
133 
134  $fieldControlResult = $this->‪renderFieldControl();
135  $fieldControlHtml = $fieldControlResult['html'];
136  $this->resultArray = $this->‪mergeChildReturnIntoExistingResult($this->resultArray, $fieldControlResult, false);
137 
138  $fieldWizardResult = $this->‪renderFieldWizard();
139  $fieldWizardHtml = $fieldWizardResult['html'];
140  $this->resultArray = $this->‪mergeChildReturnIntoExistingResult($this->resultArray, $fieldWizardResult, false);
141 
142  $html = [];
143  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
144  $html[] = $fieldInformationHtml;
145  $html[] = '<div class="form-control-wrap">';
146  $html[] = '<div class="form-wizards-wrap">';
147  $html[] = '<div class="form-wizards-element">';
148  $html[] = '<div class="t3editor-wrapper">';
149  $html[] = $editorHtml;
150  $html[] = '</div>';
151  $html[] = '</div>';
152  if (!empty($fieldControlHtml)) {
153  $html[] = '<div class="form-wizards-items-aside">';
154  $html[] = '<div class="btn-group">';
155  $html[] = $fieldControlHtml;
156  $html[] = '</div>';
157  $html[] = '</div>';
158  }
159  if (!empty($fieldWizardHtml)) {
160  $html[] = '<div class="form-wizards-items-bottom">';
161  $html[] = $fieldWizardHtml;
162  $html[] = '</div>';
163  }
164  $html[] = '</div>';
165  $html[] = '</div>';
166  $html[] = '</div>';
167 
168  $this->resultArray['html'] = implode(LF, $html);
169 
170  return ‪$this->resultArray;
171  }
172 
186  protected function ‪getHTMLCodeForEditor(
187  string $name,
188  string $class = '',
189  string $content = '',
190  string $additionalParams = '',
191  string $label = '',
192  array $hiddenfields = []
193  ): string {
194  $code = [];
195  $attributes = [];
196  ‪$mode = $this->‪getMode();
197  $registeredAddons = ‪AddonRegistry::getInstance()->getForMode(‪$mode->getFormatCode());
198 
199  $attributes['class'] = $class . ' t3editor';
200  $attributes['id'] = 't3editor_' . md5($name);
201  $attributes['name'] = $name;
202 
203  $settings = ‪AddonRegistry::getInstance()->compileSettings($registeredAddons);
204  $addons = [];
205  foreach ($registeredAddons as $addon) {
206  $addons[] = $addon->getIdentifier();
207  }
208 
209  $attributes['data-codemirror-config'] = json_encode([
210  'mode' => ‪$mode->getIdentifier(),
211  'label' => $label,
212  'addons' => json_encode($addons),
213  'options' => json_encode($settings)
214  ]);
215 
216  $attributesString = '';
217  foreach ($attributes as $attribute => $value) {
218  $attributesString .= $attribute . '="' . htmlspecialchars((string)$value) . '" ';
219  }
220  $attributesString .= $additionalParams;
221 
222  $code[] = '<textarea ' . $attributesString . '>' . 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  return implode(LF, $code);
230  }
231 
236  protected function ‪getMode(): ‪Mode
237  {
238  $config = $this->data['parameterArray']['fieldConf']['config'];
239 
240  if (!isset($config['format'])) {
241  return ‪ModeRegistry::getInstance()->getDefaultMode();
242  }
243 
244  $identifier = $config['format'];
245  if (strpos($config['format'], '/') !== false) {
246  $parts = explode('/', $config['format']);
247  $identifier = end($parts);
248  }
249 
250  return ‪ModeRegistry::getInstance()->getByFormatCode($identifier);
251  }
252 }
‪TYPO3\CMS\T3editor\Form\Element\T3editorElement\$mode
‪string $mode
Definition: T3editorElement.php:41
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:72
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪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\T3editor\Registry\AddonRegistry
Definition: AddonRegistry.php:29
‪TYPO3\CMS\T3editor\Form\Element\T3editorElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: T3editorElement.php:47
‪TYPO3\CMS\T3editor\Registry\AddonRegistry\getInstance
‪static self getInstance()
Definition: AddonRegistry.php:38
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:32
‪TYPO3\CMS\T3editor\Form\Element
Definition: T3editorElement.php:18
‪TYPO3\CMS\T3editor\Form\Element\T3editorElement
Definition: T3editorElement.php:34
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:88
‪TYPO3\CMS\T3editor\Form\Element\T3editorElement\getMode
‪Mode getMode()
Definition: T3editorElement.php:232
‪TYPO3\CMS\T3editor\Form\Element\T3editorElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: T3editorElement.php:57
‪TYPO3\CMS\T3editor\T3editor
Definition: T3editor.php:35
‪TYPO3\CMS\T3editor\Mode
Definition: Mode.php:25
‪TYPO3\CMS\T3editor\Form\Element\T3editorElement\getHTMLCodeForEditor
‪string getHTMLCodeForEditor(string $name, string $class='', string $content='', string $additionalParams='', string $label='', array $hiddenfields=[])
Definition: T3editorElement.php:182
‪TYPO3\CMS\T3editor\Exception\InvalidModeException
Definition: InvalidModeException.php:27
‪TYPO3\CMS\T3editor\Registry\ModeRegistry
Definition: ModeRegistry.php:30
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\T3editor\Form\Element\T3editorElement\$resultArray
‪array $resultArray
Definition: T3editorElement.php:37
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getInstance
‪static self getInstance()
Definition: ModeRegistry.php:43
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:104
‪TYPO3\CMS\T3editor\Form\Element\T3editorElement\render
‪array render()
Definition: T3editorElement.php:83