e0a96908f05a480d20b8b03f152e13844ada9fd9
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / TcaColumnsProcessFieldLabels.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 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Works on processedTca to determine the final value of field labels.
23 *
24 * processedTca['columns]['aField']['label']
25 */
26 class TcaColumnsProcessFieldLabels implements FormDataProviderInterface
27 {
28 /**
29 * Iterate over all processedTca columns fields
30 *
31 * @param array $result Result array
32 * @return array Modified result array
33 */
34 public function addData(array $result)
35 {
36 $result = $this->setLabelFromShowitemAndPalettes($result);
37 $result = $this->setLabelFromPageTsConfig($result);
38 $result = $this->translateLabels($result);
39 return $result;
40 }
41
42 /**
43 * The label of a single field can be set in the showitem configuration
44 * of the record type and as palettes showitem as second ";" separated argument:
45 *
46 * processedTca['types']['aType']['showitem'] = 'aFieldName;aLabelOverride, --palette--;;aPaletteName'
47 * processedTca['palettes']['aPaletteName']['showitem'] = 'anotherFieldName;anotherLabelOverride'
48 *
49 *
50 * @param array $result Result array
51 * @return array Modified result array
52 */
53 protected function setLabelFromShowitemAndPalettes(array $result)
54 {
55 $recordTypeValue = $result['recordTypeValue'];
56 // flex forms don't have a showitem / palettes configuration - early return
57 if (!isset($result['processedTca']['types'][$recordTypeValue]['showitem'])) {
58 return $result;
59 }
60 $showItemArray = GeneralUtility::trimExplode(',', $result['processedTca']['types'][$recordTypeValue]['showitem']);
61 foreach ($showItemArray as $aShowItemFieldString) {
62 $aShowItemFieldArray = GeneralUtility::trimExplode(';', $aShowItemFieldString);
63 $aShowItemFieldArray = [
64 'fieldName' => $aShowItemFieldArray[0],
65 'fieldLabel' => $aShowItemFieldArray[1] ?: null,
66 'paletteName' => $aShowItemFieldArray[2] ?: null,
67 ];
68 if ($aShowItemFieldArray['fieldName'] === '--div--') {
69 // tabs are not of interest here
70 continue;
71 } elseif ($aShowItemFieldArray['fieldName'] === '--palette--') {
72 // showitem references to a palette field. unpack the palette and process
73 // label overrides that may be in there.
74 if (!isset($result['processedTca']['palettes'][$aShowItemFieldArray['paletteName']]['showitem'])) {
75 // No palette with this name found? Skip it.
76 continue;
77 }
78 $palettesArray = GeneralUtility::trimExplode(
79 ',',
80 $result['processedTca']['palettes'][$aShowItemFieldArray['paletteName']]['showitem']
81 );
82 foreach ($palettesArray as $aPalettesString) {
83 $aPalettesArray = GeneralUtility::trimExplode(';', $aPalettesString);
84 $aPalettesArray = [
85 'fieldName' => $aPalettesArray[0],
86 'fieldLabel' => $aPalettesArray[1] ?: null,
87 ];
88 if (!empty($aPalettesArray['fieldLabel'])
89 && isset($result['processedTca']['columns'][$aPalettesArray['fieldName']])
90 ) {
91 $result['processedTca']['columns'][$aPalettesArray['fieldName']]['label'] = $aPalettesArray['fieldLabel'];
92 }
93 }
94 } else {
95 // If the field has a label in the showitem configuration of this record type, use it.
96 // showitem = 'aField, aFieldWithLabelOverride;theLabel, anotherField'
97 if (!empty($aShowItemFieldArray['fieldLabel'])
98 && isset($result['processedTca']['columns'][$aShowItemFieldArray['fieldName']])
99 ) {
100 $result['processedTca']['columns'][$aShowItemFieldArray['fieldName']]['label'] = $aShowItemFieldArray['fieldLabel'];
101 }
102 }
103 }
104 return $result;
105 }
106
107 /**
108 * pageTsConfig can override labels:
109 *
110 * TCEFORM.aTable.aField.label = 'override'
111 * TCEFORM.aTable.aField.label.en = 'override'
112 *
113 * @param array $result Result array
114 * @return array Modified result array
115 */
116 protected function setLabelFromPageTsConfig(array $result)
117 {
118 $languageService = $this->getLanguageService();
119 $table = $result['tableName'];
120 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfiguration) {
121 $fieldTSConfig = [];
122 if (isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.'])
123 && is_array($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.'])
124 ) {
125 $fieldTSConfig = $result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.'];
126 }
127 if (!empty($fieldTSConfig['label'])) {
128 $result['processedTca']['columns'][$fieldName]['label'] = $fieldTSConfig['label'];
129 }
130 if (!empty($fieldTSConfig['label.'][$languageService->lang])) {
131 $result['processedTca']['columns'][$fieldName]['label'] = $fieldTSConfig['label.'][$languageService->lang];
132 }
133 }
134 return $result;
135 }
136
137 /**
138 * Translate all labels if needed.
139 *
140 * @param array $result Result array
141 * @return array Modified result array
142 */
143 protected function translateLabels(array $result)
144 {
145 $languageService = $this->getLanguageService();
146 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfiguration) {
147 if (!isset($fieldConfiguration['label'])) {
148 continue;
149 }
150 $result['processedTca']['columns'][$fieldName]['label'] = $languageService->sL($fieldConfiguration['label']);
151 }
152 return $result;
153 }
154
155 /**
156 * @return LanguageService
157 */
158 protected function getLanguageService()
159 {
160 return $GLOBALS['LANG'];
161 }
162 }