TYPO3  7.6
TextElement.php
Go to the documentation of this file.
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 
22 
27 {
34  protected $charactersPerRow = 40;
35 
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;
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  $classes[] = 'formengine-textarea';
129  if ($specialConfiguration['fixed-font']) {
130  $classes[] = 'text-monospace';
131  }
132  if ($specialConfiguration['enable-tab']) {
133  $classes[] = 't3js-enable-tab';
134  }
135 
136  // calculate styles
137  $styles = array();
138  // add the max-height from the users' preference to it
139  $maximumHeight = (int)$backendUser->uc['resizeTextareas_MaxHeight'];
140  if ($maximumHeight > 0) {
141  $styles[] = 'max-height: ' . $maximumHeight . 'px';
142  }
143 
144  // calculate attributes
145  $attributes['id'] = StringUtility::getUniqueId('formengine-textarea-');
146  $attributes['name'] = $parameterArray['itemFormElName'];
147  if (!empty($styles)) {
148  $attributes['style'] = implode(' ', $styles);
149  }
150  if (!empty($classes)) {
151  $attributes['class'] = implode(' ', $classes);
152  }
153  $attributes['rows'] = $rows;
154  $attributes['wrap'] = $specialConfiguration['nowrap'] ? 'off' : ($config['wrap'] ?: 'virtual');
155  $attributes['onChange'] = implode('', $parameterArray['fieldChangeFunc']);
156  if (isset($config['max']) && (int)$config['max'] > 0) {
157  $attributes['maxlength'] = (int)$config['max'];
158  }
159  $attributeString = '';
160  foreach ($attributes as $attributeName => $attributeValue) {
161  $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
162  }
163 
164  // Build the textarea
165  $placeholderAttribute = '';
166  if (!empty($config['placeholder'])) {
167  $placeholderAttribute = ' placeholder="' . htmlspecialchars(trim($config['placeholder'])) . '" ';
168  }
169 
170  $html .= '<textarea'
171  . $attributeString
172  . $placeholderAttribute
173  . $parameterArray['onFocus']
174  . '>' . htmlspecialchars($parameterArray['itemFormElValue']) . '</textarea>';
175 
176  // Wrap a wizard around the item?
177  $html = $this->renderWizards(
178  array($html),
179  $config['wizards'],
180  $table,
181  $row,
182  $fieldName,
183  $parameterArray,
184  $parameterArray['itemFormElName'],
185  $specialConfiguration,
186  false
187  );
188 
189  $maximumWidth = (int)$this->formMaxWidth($cols);
190  $html = '<div class="form-control-wrap"' . ($maximumWidth ? ' style="max-width: ' . $maximumWidth . 'px"' : '') . '>' . $html . '</div>';
191  }
192 
193  $resultArray['html'] = $html;
194  return $resultArray;
195  }
196 
200  protected function getBackendUserAuthentication()
201  {
202  return $GLOBALS['BE_USER'];
203  }
204 }