‪TYPO3CMS  ‪main
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 {
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'] ?? null)) {
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]['label'])) {
61  $items[$itemKey]['label'] = $languageService->sL($label);
62  }
63  }
64  }
65 
66  $result['processedTca']['columns'][$fieldName]['config']['items'] = $items;
67  }
68 
69  return $result;
70  }
71 
76  private function ‪sanitizeConfiguration(array $config, string $fieldName, string $tableName)
77  {
78  $newItems = [];
79  foreach ($config['items'] as $itemKey => $checkboxEntry) {
80  $this->‪basicChecks($fieldName, $tableName, $checkboxEntry, $itemKey);
81  $newItems[$itemKey] = [
82  'label' => $this->‪getLanguageService()->sL(trim($checkboxEntry['label'])),
83  ];
84  if (isset($config['renderType']) && $config['renderType'] === 'checkboxToggle') {
85  $newItems = $this->‪sanitizeToggleCheckbox($checkboxEntry, $itemKey, $newItems);
86  } elseif (isset($config['renderType']) && $config['renderType'] === 'checkboxLabeledToggle') {
87  $newItems = $this->‪sanitizeLabeledToggleCheckbox($checkboxEntry, $itemKey, $newItems);
88  } else {
89  $newItems = $this->‪sanitizeIconToggleCheckbox($checkboxEntry, $itemKey, $newItems);
90  }
91  }
92  return $newItems;
93  }
94 
99  private function ‪basicChecks(string $fieldName, string $tableName, $checkboxEntry, int $checkboxKey)
100  {
101  if (!is_array($checkboxEntry)) {
102  throw new \UnexpectedValueException(
103  'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' is not an array as expected',
104  1440499337
105  );
106  }
107  if (!array_key_exists('label', $checkboxEntry)) {
108  throw new \UnexpectedValueException(
109  'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' has no label',
110  1440499338
111  );
112  }
113  }
114 
118  private function ‪sanitizeToggleCheckbox(array $item, int $itemKey, array $newItems)
119  {
120  if (array_key_exists('invertStateDisplay', $item)) {
121  $newItems[$itemKey]['invertStateDisplay'] = (bool)$item['invertStateDisplay'];
122  } else {
123  $newItems[$itemKey]['invertStateDisplay'] = false;
124  }
125  return $newItems;
126  }
127 
131  private function ‪sanitizeLabeledToggleCheckbox(array $item, int $itemKey, array $newItems)
132  {
133  if (array_key_exists('labelChecked', $item)) {
134  $newItems[$itemKey]['labelChecked'] = $this->‪getLanguageService()->sL($item['labelChecked']);
135  }
136  if (array_key_exists('labelUnchecked', $item)) {
137  $newItems[$itemKey]['labelUnchecked'] = $this->‪getLanguageService()->sL($item['labelUnchecked']);
138  }
139  if (array_key_exists('invertStateDisplay', $item)) {
140  $newItems[$itemKey]['invertStateDisplay'] = (bool)$item['invertStateDisplay'];
141  } else {
142  $newItems[$itemKey]['invertStateDisplay'] = false;
143  }
144  return $newItems;
145  }
146 
150  private function ‪sanitizeIconToggleCheckbox(array $item, int $itemKey, array $newItems)
151  {
152  if (array_key_exists('iconIdentifierChecked', $item)) {
153  $newItems[$itemKey]['iconIdentifierChecked'] = $item['iconIdentifierChecked'];
154  }
155  if (array_key_exists('iconIdentifierUnchecked', $item)) {
156  $newItems[$itemKey]['iconIdentifierUnchecked'] = $item['iconIdentifierUnchecked'];
157  }
158  if (array_key_exists('invertStateDisplay', $item)) {
159  $newItems[$itemKey]['invertStateDisplay'] = (bool)$item['invertStateDisplay'];
160  } else {
161  $newItems[$itemKey]['invertStateDisplay'] = false;
162  }
163  return $newItems;
164  }
165 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\sanitizeConfiguration
‪array sanitizeConfiguration(array $config, string $fieldName, string $tableName)
Definition: TcaCheckboxItems.php:76
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\sanitizeLabeledToggleCheckbox
‪array sanitizeLabeledToggleCheckbox(array $item, int $itemKey, array $newItems)
Definition: TcaCheckboxItems.php:131
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\addData
‪array addData(array $result)
Definition: TcaCheckboxItems.php:31
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\sanitizeIconToggleCheckbox
‪array sanitizeIconToggleCheckbox(array $item, int $itemKey, array $newItems)
Definition: TcaCheckboxItems.php:150
‪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:118
‪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\AbstractItemProvider\getLanguageService
‪getLanguageService()
Definition: AbstractItemProvider.php:1140
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems\basicChecks
‪basicChecks(string $fieldName, string $tableName, $checkboxEntry, int $checkboxKey)
Definition: TcaCheckboxItems.php:99
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider
Definition: AbstractItemProvider.php:50