0c5f150fbe1b5f0476ab288004221ac53bc55952
[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 {
43 $languageService = $this->getLanguageService();
44
45 $table = $this->data['tableName'];
46 $fieldName = $this->data['fieldName'];
47 $row = $this->data['databaseRow'];
48 $parameterArray = $this->data['parameterArray'];
49 $resultArray = $this->initializeResultArray();
50 $backendUser = $this->getBackendUserAuthentication();
51
52 $config = $parameterArray['fieldConf']['config'];
53
54 // Setting columns number
55 $cols = MathUtility::forceIntegerInRange($config['cols'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
56
57 // Setting number of rows
58 $rows = MathUtility::forceIntegerInRange($config['rows'] ?: 5, 1, 20);
59 $originalRows = $rows;
60
61 $itemFormElementValueLength = strlen($parameterArray['itemFormElValue']);
62 if ($itemFormElementValueLength > $this->charactersPerRow * 2) {
63 $cols = $this->maxInputWidth;
64 $rows = MathUtility::forceIntegerInRange(
65 round($itemFormElementValueLength / $this->charactersPerRow),
66 count(explode(LF, $parameterArray['itemFormElValue'])),
67 20
68 );
69 if ($rows < $originalRows) {
70 $rows = $originalRows;
71 }
72 }
73
74 // must be called after the cols and rows calculation, so the parameters are applied
75 // to read-only fields as well.
76 // @todo: Same as in InputTextElement ...
77 if ($config['readOnly']) {
78 $config['cols'] = $cols;
79 $config['rows'] = $rows;
80 $options = $this->data;
81 $options['parameterArray'] = array(
82 'fieldConf' => array(
83 'config' => $config,
84 ),
85 'itemFormElValue' => $parameterArray['itemFormElValue'],
86 );
87 $options['renderType'] = 'none';
88 return $this->nodeFactory->create($options)->render();
89 }
90
91 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
92 // "Extra" configuration; Returns configuration for the field based on settings found in the "types" fieldlist. Traditionally, this is where RTE configuration has been found.
93 $specialConfiguration = BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras']);
94 $html = '';
95
96 // Show message, if no RTE (field can only be edited with RTE!)
97 if ($specialConfiguration['rte_only']) {
98 $html = '<p><em>' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.noRTEfound')) . '</em></p>';
99 } else {
100 $attributes = array();
101 // validation
102 foreach ($evalList as $func) {
103 if ($func === 'required') {
104 $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString(array('required' => true));
105 } else {
106 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
107 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
108 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
109 // There is a similar hook for "evaluateFieldValue" in DataHandler and InputTextElement
110 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
111 if (class_exists($func)) {
112 $evalObj = GeneralUtility::makeInstance($func);
113 if (method_exists($evalObj, 'deevaluateFieldValue')) {
114 $_params = array(
115 'value' => $parameterArray['itemFormElValue']
116 );
117 $parameterArray['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
118 }
119 }
120 }
121 }
122 }
123
124 // calculate classes
125 $classes = array();
126 $classes[] = 'form-control';
127 $classes[] = 't3js-formengine-textarea';
128 if ($specialConfiguration['fixed-font']) {
129 $classes[] = 'text-monospace';
130 }
131 if ($specialConfiguration['enable-tab']) {
132 $classes[] = 't3js-enable-tab';
133 }
134
135 // calculate styles
136 $styles = array();
137 // add the max-height from the users' preference to it
138 $maximumHeight = (int)$backendUser->uc['resizeTextareas_MaxHeight'];
139 if ($maximumHeight > 0) {
140 $styles[] = 'max-height: ' . $maximumHeight . 'px';
141 }
142
143 // calculate attributes
144 $attributes['id'] = StringUtility::getUniqueId('formengine-textarea-');
145 $attributes['name'] = $parameterArray['itemFormElName'];
146 if (!empty($styles)) {
147 $attributes['style'] = implode(' ', $styles);
148 }
149 if (!empty($classes)) {
150 $attributes['class'] = implode(' ', $classes);
151 }
152 $attributes['rows'] = $rows;
153 $attributes['wrap'] = $specialConfiguration['nowrap'] ? 'off' : ($config['wrap'] ?: 'virtual');
154 $attributes['onChange'] = implode('', $parameterArray['fieldChangeFunc']);
155 if (isset($config['max']) && (int)$config['max'] > 0) {
156 $attributes['maxlength'] = (int)$config['max'];
157 }
158 $attributeString = '';
159 foreach ($attributes as $attributeName => $attributeValue) {
160 $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
161 }
162
163 // Build the textarea
164 $placeholderAttribute = '';
165 if (!empty($config['placeholder'])) {
166 $placeholderAttribute = ' placeholder="' . htmlspecialchars(trim($config['placeholder'])) . '" ';
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 {
201 return $GLOBALS['BE_USER'];
202 }
203 }