[FEATURE] Integrate ckeditor in the TYPO3 Backend
[Packages/TYPO3.CMS.git] / typo3 / sysext / rte_ckeditor / Classes / Form / Element / RichTextElement.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\RteCKEditor\Form\Element;
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
18 use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
22 use TYPO3\CMS\Core\Utility\PathUtility;
23 use TYPO3\CMS\Lang\LanguageService;
24
25 /**
26 * Render rich text editor in FormEngine
27 */
28 class RichTextElement extends AbstractFormElement
29 {
30
31 /**
32 * Renders the ckeditor element
33 *
34 * @return array
35 */
36 public function render() : array
37 {
38 $resultArray = $this->initializeResultArray();
39
40 $resourcesPath = PathUtility::getAbsoluteWebPath(
41 ExtensionManagementUtility::extPath('rte_ckeditor', 'Resources/Public/')
42 );
43 $table = $this->data['tableName'];
44 $row = $this->data['databaseRow'];
45 $parameterArray = $this->data['parameterArray'];
46 $defaultExtras = BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras']);
47 BackendUtility::fixVersioningPid($table, $row);
48
49 $fieldId = $this->sanitizeFieldId($parameterArray['itemFormElName']);
50 $resultArray['html'] = $this->renderWizards(
51 [$this->getHtml($fieldId)],
52 $parameterArray['fieldConf']['config']['wizards'],
53 $table,
54 $row,
55 $this->data['fieldName'],
56 $parameterArray,
57 $parameterArray['itemFormElName'],
58 $defaultExtras,
59 true
60 );
61 $resultArray['requireJsModules'] = [];
62 $resultArray['requireJsModules'][] =[
63 'ckeditor' => $this->getCkEditorRequireJsModuleCode($resourcesPath, $fieldId)
64 ];
65
66 return $resultArray;
67 }
68
69 /**
70 * Gets the JavaScript code for ckeditor module
71 *
72 * @param string $resourcesPath
73 * @param string $fieldId
74 * @return string
75 */
76 protected function getCkEditorRequireJsModuleCode(string $resourcesPath, string $fieldId) : string
77 {
78 return 'function(CKEDITOR) {
79 CKEDITOR.config.height = 400;
80 CKEDITOR.contentsCss = "' . $resourcesPath . 'Css/contents.css";
81 CKEDITOR.config.width = "auto";
82 CKEDITOR.replace("' . $fieldId . '", {
83 contentsCss: "' . $resourcesPath . 'Css/contents.css",
84 customConfig: "' . $resourcesPath . 'JavaScript/defaultconfig.js",
85 toolbar : "Basic",
86 uiColor : "#F8F8F8",
87 stylesSet: "default"
88 });
89 }';
90 }
91
92 /**
93 * Create <textarea> element
94 *
95 * @param string $fieldId
96 * @return string Main HTML to render
97 */
98 protected function getHtml(string $fieldId) : string
99 {
100 $itemFormElementName = $this->data['parameterArray']['itemFormElName'];
101 $value = $this->data['parameterArray']['itemFormElValue'] ?? '';
102
103 return '<textarea ' . $this->getValidationDataAsDataAttribute($this->data['parameterArray']['fieldConf']['config']) . ' id="' . $fieldId . '" name="' . htmlspecialchars($itemFormElementName) . '">' . htmlspecialchars($value) . '</textarea>';
104 }
105
106 /**
107 * @return LanguageService
108 */
109 protected function getLanguageService() : LanguageService
110 {
111 return $GLOBALS['LANG'];
112 }
113
114 /**
115 * @return BackendUserAuthentication
116 */
117 protected function getBackendUserAuthentication() : BackendUserAuthentication
118 {
119 return $GLOBALS['BE_USER'];
120 }
121
122 /**
123 * @param string $itemFormElementName
124 * @return string
125 */
126 protected function sanitizeFieldId(string $itemFormElementName) : string
127 {
128 $fieldId = preg_replace('/[^a-zA-Z0-9_:.-]/', '_', $itemFormElementName);
129 return htmlspecialchars(preg_replace('/^[^a-zA-Z]/', 'x', $fieldId));
130 }
131 }