‪TYPO3CMS  ‪main
TextTableElement.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 
24 
30 {
34  protected int ‪$numNewRows = 1;
35 
41  protected ‪$defaultFieldInformation = [
42  'tcaDescription' => [
43  'renderType' => 'tcaDescription',
44  ],
45  ];
46 
52  protected ‪$defaultFieldWizard = [
53  'localizationStateSelector' => [
54  'renderType' => 'localizationStateSelector',
55  ],
56  'otherLanguageContent' => [
57  'renderType' => 'otherLanguageContent',
58  'after' => [
59  'localizationStateSelector',
60  ],
61  ],
62  'defaultLanguageDifferences' => [
63  'renderType' => 'defaultLanguageDifferences',
64  'after' => [
65  'otherLanguageContent',
66  ],
67  ],
68  ];
69 
76  protected ‪$charactersPerRow = 40;
77 
83  public function ‪render(): array
84  {
85  $parameterArray = $this->data['parameterArray'];
86  $resultArray = $this->‪initializeResultArray();
87 
88  $itemValue = $parameterArray['itemFormElValue'];
89  $config = $parameterArray['fieldConf']['config'];
90  $evalList = ‪GeneralUtility::trimExplode(',', $config['eval'] ?? '', true);
91 
92  // Setting number of rows
93  $rows = $config['rows'] ?? 0;
94  $rows = ‪MathUtility::forceIntegerInRange($rows ?: 5, 1, 20);
95  $originalRows = $rows;
96  $itemFormElementValueLength = strlen($itemValue);
97  if ($itemFormElementValueLength > $this->charactersPerRow * 2) {
99  (int)round($itemFormElementValueLength / $this->charactersPerRow),
100  count(explode(LF, $itemValue)),
101  20
102  );
103  if ($rows < $originalRows) {
104  $rows = $originalRows;
105  }
106  }
107  $fieldId = ‪StringUtility::getUniqueId('formengine-textarea-');
108 
109  $fieldInformationResult = $this->‪renderFieldInformation();
110  $fieldInformationHtml = $fieldInformationResult['html'];
111  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
112 
113  if ($config['readOnly'] ?? false) {
114  $html = [];
115  $html[] = $this->‪renderLabel($fieldId);
116  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
117  $html[] = $fieldInformationHtml;
118  $html[] = '<div class="form-wizards-wrap">';
119  $html[] = '<div class="form-wizards-element">';
120  $html[] = '<div class="form-control-wrap" style="overflow: auto;">';
121  $html[] = '<textarea class="form-control" id="' . htmlspecialchars($fieldId) . '" rows="' . $rows . '" disabled>';
122  $html[] = htmlspecialchars($itemValue);
123  $html[] = '</textarea>';
124  $html[] = '</div>';
125  $html[] = '</div>';
126  $html[] = '</div>';
127  $html[] = '</div>';
128  $resultArray['html'] = implode(LF, $html);
129  return $resultArray;
130  }
131 
132  // @todo: The whole eval handling is a mess and needs refactoring
133  foreach ($evalList as $func) {
134  // @todo: This is ugly: The code should find out on it's own whether an eval definition is a
135  // @todo: keyword like "date", or a class reference. The global registration could be dropped then
136  // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
137  // There is a similar hook for "evaluateFieldValue" in DataHandler and InputTextElement
138  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
139  if (class_exists($func)) {
140  $evalObj = GeneralUtility::makeInstance($func);
141  if (method_exists($evalObj, 'deevaluateFieldValue')) {
142  $_params = [
143  'value' => $itemValue,
144  ];
145  $itemValue = $evalObj->deevaluateFieldValue($_params);
146  }
147  }
148  }
149  }
150 
151  $attributes = array_merge(
152  [
153  'id' => $fieldId,
154  'name' => htmlspecialchars($parameterArray['itemFormElName']),
155  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
156  'data-formengine-input-name' => htmlspecialchars($parameterArray['itemFormElName']),
157  'rows' => (string)$rows,
158  'wrap' => (string)(($config['wrap'] ?? 'virtual') ?: 'virtual'),
159  ],
160  $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
161  );
162  $classes = [
163  'form-control',
164  't3js-formengine-textarea',
165  'formengine-textarea',
166  ];
167  if ($config['fixedFont'] ?? false) {
168  $classes[] = 'font-monospace';
169  }
170  if ($config['enableTabulator'] ?? false) {
171  $classes[] = 't3js-enable-tab';
172  }
173  $attributes['class'] = implode(' ', $classes);
174 
175  if (isset($config['max']) && (int)$config['max'] > 0) {
176  $attributes['maxlength'] = (string)(int)$config['max'];
177  }
178  if (!empty($config['placeholder'])) {
179  $attributes['placeholder'] = htmlspecialchars(trim($config['placeholder']));
180  }
181 
182  $fieldControlResult = $this->‪renderFieldControl();
183  $fieldControlHtml = $fieldControlResult['html'];
184  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
185 
186  $fieldWizardResult = $this->‪renderFieldWizard();
187  $fieldWizardHtml = $fieldWizardResult['html'];
188  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
189 
190  $html = [];
191  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
192  $html[] = $fieldInformationHtml;
193  $html[] = '<div class="form-control-wrap" style="overflow: auto">';
194  $html[] = '<div class="form-wizards-wrap">';
195  $html[] = '<div hidden>';
196  $html[] = '<textarea ' . GeneralUtility::implodeAttributes($attributes, true) . '>' . htmlspecialchars($itemValue) . '</textarea>';
197  $html[] = '</div>';
198  $html[] = $this->‪getTableWizard($attributes['id']);
199  if (!empty($fieldControlHtml)) {
200  $html[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
201  $html[] = '<div class="btn-group">';
202  $html[] = $fieldControlHtml;
203  $html[] = '</div>';
204  $html[] = '</div>';
205  }
206  if (!empty($fieldWizardHtml)) {
207  $html[] = '<div class="form-wizards-items-bottom">';
208  $html[] = $fieldWizardHtml;
209  $html[] = '</div>';
210  }
211  $html[] = '</div>';
212  $html[] = '</div>';
213  $html[] = '</div>';
214 
215  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create(
216  '@typo3/backend/form-engine/element/text-table-element.js'
217  )->instance($fieldId);
218 
219  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/element/table-wizard-element.js');
220  $resultArray['additionalInlineLanguageLabelFiles'][] = 'EXT:core/Resources/Private/Language/locallang_wizards.xlf';
221 
222  $resultArray['html'] = $this->‪wrapWithFieldsetAndLegend(implode(LF, $html));
223  return $resultArray;
224  }
225 
231  protected function ‪getTableWizard(string $dataId): string
232  {
233  $row = $this->data['databaseRow'];
234  $delimiter = ($row['table_delimiter'][0] ?? false) ? chr((int)$row['table_delimiter'][0]) : '|';
235  $enclosure = ($row['table_enclosure'][0] ?? false) ? chr((int)$row['table_enclosure'][0]) : '';
236 
237  return sprintf(
238  '<typo3-backend-table-wizard %s></typo3-backend-table-wizard>',
239  GeneralUtility::implodeAttributes([
240  'type' => 'input',
241  'append-rows' => (string)$this->numNewRows,
242  'selector' => '#' . $dataId,
243  'delimiter' => $delimiter,
244  'enclosure' => $enclosure,
245  ], true)
246  );
247  }
248 }
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Backend\Form\Element\TextTableElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: TextTableElement.php:40
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\TextTableElement\getTableWizard
‪string getTableWizard(string $dataId)
Definition: TextTableElement.php:228
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\wrapWithFieldsetAndLegend
‪wrapWithFieldsetAndLegend(string $innerHTML)
Definition: AbstractFormElement.php:133
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:89
‪TYPO3\CMS\Backend\Form\Element\TextTableElement\$numNewRows
‪int $numNewRows
Definition: TextTableElement.php:34
‪TYPO3\CMS\Backend\Form\Element\TextTableElement
Definition: TextTableElement.php:30
‪TYPO3\CMS\Backend\Form\Element\TextTableElement\render
‪array render()
Definition: TextTableElement.php:80
‪TYPO3\CMS\Backend\Form\Element\TextTableElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: TextTableElement.php:50
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:133
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderLabel
‪renderLabel(string $for)
Definition: AbstractFormElement.php:119
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:105
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77
‪TYPO3\CMS\Backend\Form\Element\TextTableElement\$charactersPerRow
‪int $charactersPerRow
Definition: TextTableElement.php:73