‪TYPO3CMS  11.5
TcaCheckboxItems.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 
19 
24 {
32  public function ‪addData(array $result)
33  {
34  $languageService = $this->‪getLanguageService();
35  $table = $result['tableName'];
36 
37  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
38  if (empty($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'check') {
39  continue;
40  }
41 
42  if (!is_array($fieldConfig['config']['items'] ?? null)) {
43  $fieldConfig['config']['items'] = [];
44  }
45 
46  $config = $fieldConfig['config'];
47  $items = $this->‪sanitizeConfiguration($config, $fieldName, $table);
48 
49  // Resolve "itemsProcFunc"
50  if (!empty($config['itemsProcFunc'])) {
51  $items = $this->‪resolveItemProcessorFunction($result, $fieldName, $items);
52  // itemsProcFunc must not be used anymore
53  unset($result['processedTca']['columns'][$fieldName]['config']['itemsProcFunc']);
54  }
55 
56  // Set label overrides from pageTsConfig if given
57  if (isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
58  && \is_array($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
59  ) {
60  foreach ($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'] as $itemKey => $label) {
61  if (isset($items[$itemKey][0])) {
62  $items[$itemKey][0] = $languageService->sL($label);
63  }
64  }
65  }
66 
67  $result['processedTca']['columns'][$fieldName]['config']['items'] = $items;
68  }
69 
70  return $result;
71  }
72 
80  private function ‪sanitizeConfiguration(array $config, string $fieldName, string $tableName)
81  {
82  $newItems = [];
83  foreach ($config['items'] as $itemKey => $checkboxEntry) {
84  $this->‪basicChecks($fieldName, $tableName, $checkboxEntry, $itemKey);
85  $newItems[$itemKey] = [
86  $this->‪getLanguageService()->‪sL(trim($checkboxEntry[0])),
87  ];
88  if (isset($config['renderType']) && $config['renderType'] === 'checkboxToggle') {
89  $newItems = $this->‪sanitizeToggleCheckbox($checkboxEntry, $itemKey, $newItems);
90  } elseif (isset($config['renderType']) && $config['renderType'] === 'checkboxLabeledToggle') {
91  $newItems = $this->‪sanitizeLabeledToggleCheckbox($checkboxEntry, $itemKey, $newItems);
92  } else {
93  $newItems = $this->‪sanitizeIconToggleCheckbox($checkboxEntry, $itemKey, $newItems);
94  }
95  }
96  return $newItems;
97  }
98 
106  private function ‪basicChecks(string $fieldName, string $tableName, $checkboxEntry, int $checkboxKey)
107  {
108  if (!\is_array($checkboxEntry)) {
109  throw new \UnexpectedValueException(
110  'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' is not an array as expected',
111  1440499337
112  );
113  }
114  if (!\array_key_exists(0, $checkboxEntry)) {
115  throw new \UnexpectedValueException(
116  'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' has no label',
117  1440499338
118  );
119  }
120  }
121 
128  private function ‪sanitizeToggleCheckbox(array $item, int $itemKey, array $newItems)
129  {
130  if (array_key_exists('invertStateDisplay', $item)) {
131  $newItems[$itemKey]['invertStateDisplay'] = (bool)$item['invertStateDisplay'];
132  } else {
133  $newItems[$itemKey]['invertStateDisplay'] = false;
134  }
135  return $newItems;
136  }
137 
144  private function ‪sanitizeLabeledToggleCheckbox(array $item, int $itemKey, array $newItems)
145  {
146  if (array_key_exists('labelChecked', $item)) {
147  $newItems[$itemKey]['labelChecked'] = $this->‪getLanguageService()->‪sL($item['labelChecked']);
148  }
149  if (array_key_exists('labelUnchecked', $item)) {
150  $newItems[$itemKey]['labelUnchecked'] = $this->‪getLanguageService()->‪sL($item['labelUnchecked']);
151  }
152  if (array_key_exists('invertStateDisplay', $item)) {
153  $newItems[$itemKey]['invertStateDisplay'] = (bool)$item['invertStateDisplay'];
154  } else {
155  $newItems[$itemKey]['invertStateDisplay'] = false;
156  }
157  return $newItems;
158  }
159 
166  private function ‪sanitizeIconToggleCheckbox(array $item, int $itemKey, array $newItems)
167  {
168  if (array_key_exists('iconIdentifierChecked', $item)) {
169  $newItems[$itemKey]['iconIdentifierChecked'] = $item['iconIdentifierChecked'];
170  }
171  if (array_key_exists('iconIdentifierUnchecked', $item)) {
172  $newItems[$itemKey]['iconIdentifierUnchecked'] = $item['iconIdentifierUnchecked'];
173  }
174  if (array_key_exists('invertStateDisplay', $item)) {
175  $newItems[$itemKey]['invertStateDisplay'] = (bool)$item['invertStateDisplay'];
176  } else {
177  $newItems[$itemKey]['invertStateDisplay'] = false;
178  }
179  return $newItems;
180  }
181 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\sanitizeConfiguration
‪array sanitizeConfiguration(array $config, string $fieldName, string $tableName)
Definition: TcaCheckboxItems.php:80
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\sanitizeLabeledToggleCheckbox
‪array sanitizeLabeledToggleCheckbox(array $item, int $itemKey, array $newItems)
Definition: TcaCheckboxItems.php:144
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractItemProvider.php:1207
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\addData
‪array addData(array $result)
Definition: TcaCheckboxItems.php:32
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:161
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\sanitizeIconToggleCheckbox
‪array sanitizeIconToggleCheckbox(array $item, int $itemKey, array $newItems)
Definition: TcaCheckboxItems.php:166
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\resolveItemProcessorFunction
‪array resolveItemProcessorFunction(array $result, $fieldName, array $items)
Definition: AbstractItemProvider.php:59
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\sanitizeToggleCheckbox
‪array sanitizeToggleCheckbox(array $item, int $itemKey, array $newItems)
Definition: TcaCheckboxItems.php:128
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems
Definition: TcaCheckboxItems.php:24
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\basicChecks
‪basicChecks(string $fieldName, string $tableName, $checkboxEntry, int $checkboxKey)
Definition: TcaCheckboxItems.php:106
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider
Definition: AbstractItemProvider.php:50