‪TYPO3CMS  10.4
AbstractFormElement.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
27 
32 {
38  protected ‪$defaultInputWidth = 30;
39 
45  protected ‪$minimumInputWidth = 10;
46 
52  protected ‪$maxInputWidth = 50;
53 
57  protected ‪$iconFactory;
58 
66  {
67  parent::__construct(‪$nodeFactory, ‪$data);
68  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
69  }
70 
76  protected function ‪renderFieldInformation(): array
77  {
78  $options = ‪$this->data;
79  $fieldInformation = ‪$this->defaultFieldInformation;
80  $fieldInformationFromTca = $options['parameterArray']['fieldConf']['config']['fieldInformation'] ?? [];
81  ‪ArrayUtility::mergeRecursiveWithOverrule($fieldInformation, $fieldInformationFromTca);
82  $options['renderType'] = 'fieldInformation';
83  $options['renderData']['fieldInformation'] = $fieldInformation;
84  return $this->nodeFactory->create($options)->render();
85  }
86 
92  protected function ‪renderFieldControl(): array
93  {
94  $options = ‪$this->data;
95  $fieldControl = ‪$this->defaultFieldControl;
96  $fieldControlFromTca = $options['parameterArray']['fieldConf']['config']['fieldControl'] ?? [];
97  ‪ArrayUtility::mergeRecursiveWithOverrule($fieldControl, $fieldControlFromTca);
98  $options['renderType'] = 'fieldControl';
99  $options['renderData']['fieldControl'] = $fieldControl;
100  return $this->nodeFactory->create($options)->render();
101  }
102 
108  protected function ‪renderFieldWizard(): array
109  {
110  $options = ‪$this->data;
111  $fieldWizard = ‪$this->defaultFieldWizard;
112  $fieldWizardFromTca = $options['parameterArray']['fieldConf']['config']['fieldWizard'] ?? [];
113  ‪ArrayUtility::mergeRecursiveWithOverrule($fieldWizard, $fieldWizardFromTca);
114  $options['renderType'] = 'fieldWizard';
115  $options['renderData']['fieldWizard'] = $fieldWizard;
116  return $this->nodeFactory->create($options)->render();
117  }
118 
136  protected function ‪hasNullCheckboxButNoPlaceholder(): bool
137  {
138  $hasNullCheckboxNoPlaceholder = false;
139  $parameterArray = $this->data['parameterArray'];
140  $mode = $parameterArray['fieldConf']['config']['mode'] ?? '';
141  if (empty($this->data['flexFormDataStructureIdentifier'])
142  && !empty($parameterArray['fieldConf']['config']['eval'])
143  && GeneralUtility::inList($parameterArray['fieldConf']['config']['eval'], 'null')
144  && ($mode !== 'useOrOverridePlaceholder')
145  ) {
146  $hasNullCheckboxNoPlaceholder = true;
147  }
148  return $hasNullCheckboxNoPlaceholder;
149  }
150 
171  protected function ‪hasNullCheckboxWithPlaceholder(): bool
172  {
173  $hasNullCheckboxWithPlaceholder = false;
174  $parameterArray = $this->data['parameterArray'];
175  $mode = $parameterArray['fieldConf']['config']['mode'] ?? '';
176  if (empty($this->data['flexFormDataStructureIdentifier'])
177  && !empty($parameterArray['fieldConf']['config']['eval'])
178  && GeneralUtility::inList($parameterArray['fieldConf']['config']['eval'], 'null')
179  && ($mode === 'useOrOverridePlaceholder')
180  ) {
181  $hasNullCheckboxWithPlaceholder = true;
182  }
183  return $hasNullCheckboxWithPlaceholder;
184  }
185 
194  protected function ‪formatValue($format, $itemValue, $formatOptions = [])
195  {
196  switch ($format) {
197  case 'date':
198  if ($itemValue) {
199  $option = isset($formatOptions['option']) ? trim($formatOptions['option']) : '';
200  if ($option) {
201  if (isset($formatOptions['strftime']) && $formatOptions['strftime']) {
202  $value = strftime($option, (int)$itemValue);
203  } else {
204  $value = date($option, (int)$itemValue);
205  }
206  } else {
207  $value = date('d-m-Y', (int)$itemValue);
208  }
209  } else {
210  $value = '';
211  }
212  if (isset($formatOptions['appendAge']) && $formatOptions['appendAge']) {
214  ‪$GLOBALS['EXEC_TIME'] - $itemValue,
215  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.minutesHoursDaysYears')
216  );
217  $value .= ' (' . $age . ')';
218  }
219  $itemValue = $value;
220  break;
221  case 'datetime':
222  // compatibility with "eval" (type "input")
223  if ($itemValue !== '' && $itemValue !== null) {
224  $itemValue = ‪BackendUtility::datetime((int)$itemValue);
225  }
226  break;
227  case 'time':
228  // compatibility with "eval" (type "input")
229  if ($itemValue !== '' && $itemValue !== null) {
230  $itemValue = ‪BackendUtility::time((int)$itemValue, false);
231  }
232  break;
233  case 'timesec':
234  // compatibility with "eval" (type "input")
235  if ($itemValue !== '' && $itemValue !== null) {
236  $itemValue = ‪BackendUtility::time((int)$itemValue);
237  }
238  break;
239  case 'year':
240  // compatibility with "eval" (type "input")
241  if ($itemValue !== '' && $itemValue !== null) {
242  $itemValue = date('Y', (int)$itemValue);
243  }
244  break;
245  case 'int':
246  $baseArr = ['dec' => 'd', 'hex' => 'x', 'HEX' => 'X', 'oct' => 'o', 'bin' => 'b'];
247  $base = isset($formatOptions['base']) ? trim($formatOptions['base']) : '';
248  $format = $baseArr[$base] ?? 'd';
249  $itemValue = sprintf('%' . $format, $itemValue);
250  break;
251  case 'float':
252  // default precision
253  $precision = 2;
254  if (isset($formatOptions['precision'])) {
255  $precision = ‪MathUtility::forceIntegerInRange($formatOptions['precision'], 1, 10, $precision);
256  }
257  $itemValue = sprintf('%.' . $precision . 'f', $itemValue);
258  break;
259  case 'number':
260  $format = isset($formatOptions['option']) ? trim($formatOptions['option']) : '';
261  $itemValue = sprintf('%' . $format, $itemValue);
262  break;
263  case 'md5':
264  $itemValue = md5($itemValue);
265  break;
266  case 'filesize':
267  // We need to cast to int here, otherwise empty values result in empty output,
268  // but we expect zero.
269  $value = GeneralUtility::formatSize((int)$itemValue);
270  if (!empty($formatOptions['appendByteSize'])) {
271  $value .= ' (' . $itemValue . ')';
272  }
273  $itemValue = $value;
274  break;
275  case 'user':
276  $func = trim($formatOptions['userFunc']);
277  if ($func) {
278  $params = [
279  'value' => $itemValue,
280  'args' => $formatOptions['userFunc'],
281  'config' => [
282  'type' => 'none',
283  'format' => $format,
284  'format.' => $formatOptions,
285  ],
286  ];
287  $itemValue = GeneralUtility::callUserFunction($func, $params, $this);
288  }
289  break;
290  default:
291  // Do nothing e.g. when $format === ''
292  }
293  return $itemValue;
294  }
295 
302  protected function ‪formMaxWidth($size = 48)
303  {
304  $compensationForLargeDocuments = 1.33;
305  $compensationForFormFields = 12;
306 
307  $size = round($size * $compensationForLargeDocuments);
308  return ceil($size * $compensationForFormFields);
309  }
310 
311  /***********************************************
312  * CheckboxElement related methods
313  ***********************************************/
314 
326  protected function ‪checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript = ''): string
327  {
328  $elementName = 'document.editform[' . GeneralUtility::quoteJSvalue($itemName) . ']';
329  $checkboxPow = 2 ** $checkbox;
330  $onClick = $elementName . '.value=this.checked?(' . $elementName . '.value|' . $checkboxPow . '):('
331  . $elementName . '.value&' . ((2 ** $checkboxesCount) - 1 - $checkboxPow) . ');' . $additionalJavaScript;
332  return ' onclick="' . htmlspecialchars($onClick) . '"' . ($formElementValue & $checkboxPow ? ' checked="checked"' : '');
333  }
334 
343  protected function ‪calculateColumnMarkup(int $cols): array
344  {
345  $colWidth = (int)floor(12 / $cols);
346  $colClass = 'col-md-12';
347  $colClear = [];
348  if ($colWidth === 6) {
349  $colClass = 'col-sm-6';
350  $colClear = [
351  2 => 'visible-sm-block visible-md-block visible-lg-block',
352  ];
353  } elseif ($colWidth === 4) {
354  $colClass = 'col-sm-4';
355  $colClear = [
356  3 => 'visible-sm-block visible-md-block visible-lg-block',
357  ];
358  } elseif ($colWidth === 3) {
359  $colClass = 'col-sm-6 col-md-3';
360  $colClear = [
361  2 => 'visible-sm-block',
362  4 => 'visible-md-block visible-lg-block',
363  ];
364  } elseif ($colWidth <= 2) {
365  $colClass = 'checkbox-column col-sm-6 col-md-3 col-lg-2';
366  $colClear = [
367  2 => 'visible-sm-block',
368  4 => 'visible-md-block',
369  6 => 'visible-lg-block'
370  ];
371  }
372  return [$colClass, $colClear];
373  }
374 
382  protected function ‪appendValueToLabelInDebugMode($label, $value)
383  {
384  if ($value !== '' && ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['debug'] && $this->‪getBackendUser()->isAdmin()) {
385  return $label . ' [' . $value . ']';
386  }
387 
388  return (string)$label;
389  }
390 
394  protected function ‪getLanguageService()
395  {
396  return ‪$GLOBALS['LANG'];
397  }
398 
404  protected function ‪getBackendUser()
405  {
406  return ‪$GLOBALS['BE_USER'];
407  }
408 }
‪TYPO3\CMS\Backend\Form\AbstractNode\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: AbstractNode.php:48
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:72
‪TYPO3\CMS\Backend\Utility\BackendUtility\datetime
‪static string datetime($value)
Definition: BackendUtility.php:989
‪TYPO3\CMS\Backend\Utility\BackendUtility\calcAge
‪static string calcAge($seconds, $labels='min|hrs|days|yrs|min|hour|day|year')
Definition: BackendUtility.php:1017
‪TYPO3\CMS\Backend\Form\AbstractNode\$nodeFactory
‪NodeFactory $nodeFactory
Definition: AbstractNode.php:36
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Backend\Form\AbstractNode\$defaultFieldControl
‪array $defaultFieldControl
Definition: AbstractNode.php:55
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:32
‪TYPO3\CMS\Backend\Form\AbstractNode\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: AbstractNode.php:62
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:33
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\$defaultInputWidth
‪int $defaultInputWidth
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\$maxInputWidth
‪int $maxInputWidth
Definition: AbstractFormElement.php:49
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\__construct
‪__construct(NodeFactory $nodeFactory, array $data)
Definition: AbstractFormElement.php:61
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formatValue
‪string formatValue($format, $itemValue, $formatOptions=[])
Definition: AbstractFormElement.php:190
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\$minimumInputWidth
‪int $minimumInputWidth
Definition: AbstractFormElement.php:43
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\$iconFactory
‪IconFactory $iconFactory
Definition: AbstractFormElement.php:53
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:88
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxWithPlaceholder
‪bool hasNullCheckboxWithPlaceholder()
Definition: AbstractFormElement.php:167
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxButNoPlaceholder
‪bool hasNullCheckboxButNoPlaceholder()
Definition: AbstractFormElement.php:132
‪TYPO3\CMS\Backend\Form\AbstractNode\$data
‪array $data
Definition: AbstractNode.php:42
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:298
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:37
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\appendValueToLabelInDebugMode
‪string appendValueToLabelInDebugMode($label, $value)
Definition: AbstractFormElement.php:378
‪TYPO3\CMS\Backend\Utility\BackendUtility\time
‪static string time($value, $withSeconds=true)
Definition: BackendUtility.php:1005
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: AbstractFormElement.php:400
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractFormElement.php:390
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\calculateColumnMarkup
‪array calculateColumnMarkup(int $cols)
Definition: AbstractFormElement.php:339
‪TYPO3\CMS\Backend\Form\AbstractNode
Definition: AbstractNode.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:104
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\checkBoxParams
‪string checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript='')
Definition: AbstractFormElement.php:322