2eb3e0d6f4b90efad99a0c6792953193f9121927
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / TcaRadioItems.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 radio items and set processed item list in processedTca
22 */
23 class TcaRadioItems extends AbstractItemProvider implements FormDataProviderInterface
24 {
25 /**
26 * Resolve radio 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'] !== 'radio') {
39 continue;
40 }
41
42 if (!is_array($fieldConfig['config']['items'])) {
43 throw new \UnexpectedValueException(
44 'Radio field ' . $fieldName . ' of TCA table ' . $result['tableName'] . ' must have \'config\' \'items\' definition',
45 1438594829
46 );
47 }
48
49 $config = $fieldConfig['config'];
50 $items = $config['items'];
51
52 // Sanitize items and translate labels
53 $newItems = [];
54 foreach ($items as $itemKey => $itemValue) {
55 if (!is_array($itemValue)) {
56 throw new \UnexpectedValueException(
57 'Item ' . $itemKey . ' of field ' . $fieldName . ' of TCA table ' . $result['tableName'] . ' is no array as exepcted',
58 1438607163
59 );
60 }
61 if (!array_key_exists(0, $itemValue)) {
62 throw new \UnexpectedValueException(
63 'Item ' . $itemKey . ' of field ' . $fieldName . ' of TCA table ' . $result['tableName'] . ' has no label',
64 1438607164
65 );
66 }
67 if (!array_key_exists(1, $itemValue)) {
68 throw new \UnexpectedValueException(
69 'Item ' . $itemKey . ' of field ' . $fieldName . ' of TCA table ' . $result['tableName'] . ' has no value',
70 1438607165
71 );
72 }
73 $newItems[$itemKey] = [
74 $languageService->sL(trim($itemValue[0])),
75 $itemValue[1]
76 ];
77 }
78 $items = $newItems;
79
80 // Resolve "itemsProcFunc"
81 if (!empty($config['itemsProcFunc'])) {
82 $items = $this->resolveItemProcessorFunction($result, $fieldName, $items);
83 // itemsProcFunc must not be used anymore
84 unset($result['processedTca']['columns'][$fieldName]['config']['itemsProcFunc']);
85 }
86
87 // Set label overrides from pageTsConfig if given
88 if (isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
89 && is_array($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
90 ) {
91 foreach ($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'] as $itemKey => $label) {
92 if (isset($items[$itemKey][0])) {
93 $items[$itemKey][0] = $languageService->sL(trim($label));
94 }
95 }
96 }
97
98 $result['processedTca']['columns'][$fieldName]['config']['items'] = $items;
99 }
100
101 return $result;
102 }
103
104 /**
105 * @return LanguageService
106 */
107 protected function getLanguageService()
108 {
109 return $GLOBALS['LANG'];
110 }
111 }