[TASK] Move LanguageService to EXT:core
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / TcaCheckboxItems.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
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
17 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
18 use TYPO3\CMS\Core\Localization\LanguageService;
19
20 /**
21 * Resolve checkbox items and set processed item list in processedTca
22 */
23 class TcaCheckboxItems extends AbstractItemProvider implements FormDataProviderInterface
24 {
25 /**
26 * Resolve checkbox items
27 *
28 * @param array $result
29 * @return array
30 * @throws \UnexpectedValueException
31 */
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 = $config['items'];
48
49 // Sanitize items
50 $newItems = [];
51 foreach ($items as $itemKey => $itemValue) {
52 if (!is_array($itemValue)) {
53 throw new \UnexpectedValueException(
54 'Item ' . $itemKey . ' of field ' . $fieldName . ' of TCA table ' . $result['tableName'] . ' is no array as exepcted',
55 1440499337
56 );
57 }
58 if (!array_key_exists(0, $itemValue)) {
59 throw new \UnexpectedValueException(
60 'Item ' . $itemKey . ' of field ' . $fieldName . ' of TCA table ' . $result['tableName'] . ' has no label',
61 1440499338
62 );
63 }
64 if (!array_key_exists(1, $itemValue)) {
65 $itemValue[1] = '';
66 }
67 $newItems[$itemKey] = [
68 $languageService->sL(trim($itemValue[0])),
69 $itemValue[1]
70 ];
71 }
72 $items = $newItems;
73
74 // Resolve "itemsProcFunc"
75 if (!empty($config['itemsProcFunc'])) {
76 $items = $this->resolveItemProcessorFunction($result, $fieldName, $items);
77 // itemsProcFunc must not be used anymore
78 unset($result['processedTca']['columns'][$fieldName]['config']['itemsProcFunc']);
79 }
80
81 // Set label overrides from pageTsConfig if given
82 if (isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
83 && is_array($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
84 ) {
85 foreach ($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'] as $itemKey => $label) {
86 if (isset($items[$itemKey][0])) {
87 $items[$itemKey][0] = $languageService->sL($label);
88 }
89 }
90 }
91
92 $result['processedTca']['columns'][$fieldName]['config']['items'] = $items;
93 }
94
95 return $result;
96 }
97
98 /**
99 * @return LanguageService
100 */
101 protected function getLanguageService()
102 {
103 return $GLOBALS['LANG'];
104 }
105 }