7ace1296843105b4bfa94822e8833dbdbe39a962
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / TextElement.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\MathUtility;
20 use TYPO3\CMS\Core\Utility\StringUtility;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22
23 /**
24 * Generation of TCEform elements of the type "text"
25 */
26 class TextElement extends AbstractFormElement {
27
28 /**
29 * The number of chars expected per row when the height of a text area field is
30 * automatically calculated based on the number of characters found in the field content.
31 *
32 * @var int
33 */
34 protected $charactersPerRow = 40;
35
36 /**
37 * This will render a <textarea>
38 *
39 * @return array As defined in initializeResultArray() of AbstractNode
40 */
41 public function render() {
42 $languageService = $this->getLanguageService();
43
44 $table = $this->data['tableName'];
45 $fieldName = $this->data['fieldName'];
46 $row = $this->data['databaseRow'];
47 $parameterArray = $this->data['parameterArray'];
48 $resultArray = $this->initializeResultArray();
49 $backendUser = $this->getBackendUserAuthentication();
50
51 $config = $parameterArray['fieldConf']['config'];
52
53 // Setting columns number
54 $cols = MathUtility::forceIntegerInRange($config['cols'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
55
56 // Setting number of rows
57 $rows = MathUtility::forceIntegerInRange($config['rows'] ?: 5, 1, 20);
58 $originalRows = $rows;
59
60 $itemFormElementValueLength = strlen($parameterArray['itemFormElValue']);
61 if ($itemFormElementValueLength > $this->charactersPerRow * 2) {
62 $cols = $this->maxInputWidth;
63 $rows = MathUtility::forceIntegerInRange(
64 round($itemFormElementValueLength / $this->charactersPerRow),
65 count(explode(LF, $parameterArray['itemFormElValue'])),
66 20
67 );
68 if ($rows < $originalRows) {
69 $rows = $originalRows;
70 }
71 }
72
73 // must be called after the cols and rows calculation, so the parameters are applied
74 // to read-only fields as well.
75 // @todo: Same as in InputTextElement ...
76 if ($config['readOnly']) {
77 $config['cols'] = $cols;
78 $config['rows'] = $rows;
79 $options = $this->data;
80 $options['parameterArray'] = array(
81 'fieldConf' => array(
82 'config' => $config,
83 ),
84 'itemFormElValue' => $parameterArray['itemFormElValue'],
85 );
86 $options['renderType'] = 'none';
87 return $this->nodeFactory->create($options)->render();
88 }
89
90 $evalList = GeneralUtility::trimExplode(',', $config['eval'], TRUE);
91 // "Extra" configuration; Returns configuration for the field based on settings found in the "types" fieldlist. Traditionally, this is where RTE configuration has been found.
92 $specialConfiguration = BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras']);
93 $html = '';
94
95 // Show message, if no RTE (field can only be edited with RTE!)
96 if ($specialConfiguration['rte_only']) {
97 $html = '<p><em>' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.noRTEfound')) . '</em></p>';
98 } else {
99 $attributes = array();
100 // validation
101 foreach ($evalList as $func) {
102 if ($func === 'required') {
103 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString(array('required' => TRUE));
104 } else {
105 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
106 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
107 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
108 // There is a similar hook for "evaluateFieldValue" in DataHandler and InputTextElement
109 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
110 if (class_exists($func)) {
111 $evalObj = GeneralUtility::makeInstance($func);
112 if (method_exists($evalObj, 'deevaluateFieldValue')) {
113 $_params = array(
114 'value' => $parameterArray['itemFormElValue']
115 );
116 $parameterArray['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
117 }
118 }
119 }
120 }
121 }
122
123 // calculate classes
124 $classes = array();
125 $classes[] = 'form-control';
126 $classes[] = 't3js-formengine-textarea';
127 if ($specialConfiguration['fixed-font']) {
128 $classes[] = 'text-monospace';
129 }
130 if ($specialConfiguration['enable-tab']) {
131 $classes[] = 't3js-enable-tab';
132 }
133
134 // calculate styles
135 $styles = array();
136 // add the max-height from the users' preference to it
137 $maximumHeight = (int)$backendUser->uc['resizeTextareas_MaxHeight'];
138 if ($maximumHeight > 0) {
139 $styles[] = 'max-height: ' . $maximumHeight . 'px';
140 }
141
142 // calculate attributes
143 $attributes['id'] = StringUtility::getUniqueId('formengine-textarea-');
144 $attributes['name'] = $parameterArray['itemFormElName'];
145 if (!empty($styles)) {
146 $attributes['style'] = implode(' ', $styles);
147 }
148 if (!empty($classes)) {
149 $attributes['class'] = implode(' ', $classes);
150 }
151 $attributes['rows'] = $rows;
152 $attributes['wrap'] = $specialConfiguration['nowrap'] ? 'off' : ($config['wrap'] ?: 'virtual');
153 $attributes['onChange'] = implode('', $parameterArray['fieldChangeFunc']);
154 if (isset($config['max']) && (int)$config['max'] > 0) {
155 $attributes['maxlength'] = (int)$config['max'];
156 }
157 $attributeString = '';
158 foreach ($attributes as $attributeName => $attributeValue) {
159 $attributeString .= ' '. $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
160 }
161
162 // Build the textarea
163 $placeholderValue = $this->getPlaceholderValue($table, $config, $row);
164 $placeholderAttribute = '';
165 if (!empty($placeholderValue)) {
166 $placeholderAttribute = ' placeholder="' . htmlspecialchars(trim($languageService->sL($placeholderValue))) . '" ';
167 }
168
169 $html .= '<textarea'
170 . $attributeString
171 . $placeholderAttribute
172 . $parameterArray['onFocus']
173 . '>' . htmlspecialchars($parameterArray['itemFormElValue']) . '</textarea>';
174
175 // Wrap a wizard around the item?
176 $html = $this->renderWizards(
177 array($html),
178 $config['wizards'],
179 $table,
180 $row,
181 $fieldName,
182 $parameterArray,
183 $parameterArray['itemFormElName'],
184 $specialConfiguration,
185 FALSE
186 );
187
188 $maximumWidth = (int)$this->formMaxWidth($cols);
189 $html = '<div class="form-control-wrap"' . ($maximumWidth ? ' style="max-width: ' . $maximumWidth . 'px"' : '') . '>' . $html . '</div>';
190 }
191
192 $resultArray['html'] = $html;
193 return $resultArray;
194 }
195
196 /**
197 * @return BackendUserAuthentication
198 */
199 protected function getBackendUserAuthentication() {
200 return $GLOBALS['BE_USER'];
201 }
202
203 }