‪TYPO3CMS  9.5
TcaCheckboxItems.php
Go to the documentation of this file.
1 <?php
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 
18 
23 {
31  public function ‪addData(array $result)
32  {
33  $languageService = $this->‪getLanguageService();
34  $table = $result['tableName'];
35 
36  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
37  if (empty($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'check') {
38  continue;
39  }
40 
41  if (!is_array($fieldConfig['config']['items'])) {
42  $fieldConfig['config']['items'] = [];
43  }
44 
45  $config = $fieldConfig['config'];
46  $items = $this->‪sanitizeConfiguration($config, $fieldName, $table);
47 
48  // Resolve "itemsProcFunc"
49  if (!empty($config['itemsProcFunc'])) {
50  $items = $this->‪resolveItemProcessorFunction($result, $fieldName, $items);
51  // itemsProcFunc must not be used anymore
52  unset($result['processedTca']['columns'][$fieldName]['config']['itemsProcFunc']);
53  }
54 
55  // Set label overrides from pageTsConfig if given
56  if (isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
57  && \is_array($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
58  ) {
59  foreach ($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'] as $itemKey => $label) {
60  if (isset($items[$itemKey][0])) {
61  $items[$itemKey][0] = $languageService->sL($label);
62  }
63  }
64  }
65 
66  $result['processedTca']['columns'][$fieldName]['config']['items'] = $items;
67  }
68 
69  return $result;
70  }
71 
79  private function ‪sanitizeConfiguration(array $config, string $fieldName, string $tableName)
80  {
81  $newItems = [];
82  foreach ($config['items'] as $itemKey => $checkboxEntry) {
83  $this->‪basicChecks($fieldName, $tableName, $checkboxEntry, $itemKey);
84  $newItems[$itemKey] = [
85  $this->‪getLanguageService()->‪sL(trim($checkboxEntry[0])),
86  $checkboxEntry[1]
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:79
‪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:1389
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\addData
‪array addData(array $result)
Definition: TcaCheckboxItems.php:31
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪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:54
‪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:23
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:2
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:22
‪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:45