‪TYPO3CMS  10.4
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'])) {
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  $checkboxEntry[1]
88  ];
89  if (isset($config['renderType']) && $config['renderType'] === 'checkboxToggle') {
90  $newItems = $this->‪sanitizeToggleCheckbox($checkboxEntry, $itemKey, $newItems);
91  } elseif (isset($config['renderType']) && $config['renderType'] === 'checkboxLabeledToggle') {
92  $newItems = $this->‪sanitizeLabeledToggleCheckbox($checkboxEntry, $itemKey, $newItems);
93  } else {
94  $newItems = $this->‪sanitizeIconToggleCheckbox($checkboxEntry, $itemKey, $newItems);
95  }
96  }
97  return $newItems;
98  }
99 
107  private function ‪basicChecks(string $fieldName, string $tableName, $checkboxEntry, int $checkboxKey)
108  {
109  if (!\is_array($checkboxEntry)) {
110  throw new \UnexpectedValueException(
111  'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' is not an array as expected',
112  1440499337
113  );
114  }
115  if (!\array_key_exists(0, $checkboxEntry)) {
116  throw new \UnexpectedValueException(
117  'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' has no label',
118  1440499338
119  );
120  }
121  }
122 
129  private function ‪sanitizeToggleCheckbox(array $item, int $itemKey, array $newItems)
130  {
131  if (array_key_exists('invertStateDisplay', $item)) {
132  $newItems[$itemKey]['invertStateDisplay'] = (bool)$item['invertStateDisplay'];
133  } else {
134  $newItems[$itemKey]['invertStateDisplay'] = false;
135  }
136  return $newItems;
137  }
138 
145  private function ‪sanitizeLabeledToggleCheckbox(array $item, int $itemKey, array $newItems)
146  {
147  if (array_key_exists('labelChecked', $item)) {
148  $newItems[$itemKey]['labelChecked'] = $this->‪getLanguageService()->‪sL($item['labelChecked']);
149  }
150  if (array_key_exists('labelUnchecked', $item)) {
151  $newItems[$itemKey]['labelUnchecked'] = $this->‪getLanguageService()->‪sL($item['labelUnchecked']);
152  }
153  if (array_key_exists('invertStateDisplay', $item)) {
154  $newItems[$itemKey]['invertStateDisplay'] = (bool)$item['invertStateDisplay'];
155  } else {
156  $newItems[$itemKey]['invertStateDisplay'] = false;
157  }
158  return $newItems;
159  }
160 
167  private function ‪sanitizeIconToggleCheckbox(array $item, int $itemKey, array $newItems)
168  {
169  if (array_key_exists('iconIdentifierChecked', $item)) {
170  $newItems[$itemKey]['iconIdentifierChecked'] = $item['iconIdentifierChecked'];
171  }
172  if (array_key_exists('iconIdentifierUnchecked', $item)) {
173  $newItems[$itemKey]['iconIdentifierUnchecked'] = $item['iconIdentifierUnchecked'];
174  }
175  if (array_key_exists('invertStateDisplay', $item)) {
176  $newItems[$itemKey]['invertStateDisplay'] = (bool)$item['invertStateDisplay'];
177  } else {
178  $newItems[$itemKey]['invertStateDisplay'] = false;
179  }
180  return $newItems;
181  }
182 }
‪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:145
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractItemProvider.php:1468
‪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:194
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\sanitizeIconToggleCheckbox
‪array sanitizeIconToggleCheckbox(array $item, int $itemKey, array $newItems)
Definition: TcaCheckboxItems.php:167
‪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:129
‪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:107
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider
Definition: AbstractItemProvider.php:50