[BUGFIX] Notice free FormEngine testing
[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' => !empty($aShowItemFieldArray[1]) ? $aShowItemFieldArray[1] : null,
66 'paletteName' => !empty($aShowItemFieldArray[2]) ? $aShowItemFieldArray[2] : null,
67 ];
68 if ($aShowItemFieldArray['fieldName'] === '--div--') {
69 // tabs are not of interest here
70 continue;
71 }
72 if ($aShowItemFieldArray['fieldName'] === '--palette--') {
73 // showitem references to a palette field. unpack the palette and process
74 // label overrides that may be in there.
75 if (!isset($result['processedTca']['palettes'][$aShowItemFieldArray['paletteName']]['showitem'])) {
76 // No palette with this name found? Skip it.
77 continue;
78 }
79 $palettesArray = GeneralUtility::trimExplode(
80 ',',
81 $result['processedTca']['palettes'][$aShowItemFieldArray['paletteName']]['showitem']
82 );
83 foreach ($palettesArray as $aPalettesString) {
84 $aPalettesArray = GeneralUtility::trimExplode(';', $aPalettesString);
85 $aPalettesArray = [
86 'fieldName' => $aPalettesArray[0],
87 'fieldLabel' => $aPalettesArray[1] ?: null,
88 ];
89 if (!empty($aPalettesArray['fieldLabel'])
90 && isset($result['processedTca']['columns'][$aPalettesArray['fieldName']])
91 ) {
92 $result['processedTca']['columns'][$aPalettesArray['fieldName']]['label'] = $aPalettesArray['fieldLabel'];
93 }
94 }
95 } else {
96 // If the field has a label in the showitem configuration of this record type, use it.
97 // showitem = 'aField, aFieldWithLabelOverride;theLabel, anotherField'
98 if (!empty($aShowItemFieldArray['fieldLabel'])
99 && isset($result['processedTca']['columns'][$aShowItemFieldArray['fieldName']])
100 ) {
101 $result['processedTca']['columns'][$aShowItemFieldArray['fieldName']]['label'] = $aShowItemFieldArray['fieldLabel'];
102 }
103 }
104 }
105 return $result;
106 }
107
108 /**
109 * pageTsConfig can override labels:
110 *
111 * TCEFORM.aTable.aField.label = 'override'
112 * TCEFORM.aTable.aField.label.en = 'override'
113 *
114 * @param array $result Result array
115 * @return array Modified result array
116 */
117 protected function setLabelFromPageTsConfig(array $result)
118 {
119 $languageService = $this->getLanguageService();
120 $table = $result['tableName'];
121 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfiguration) {
122 $fieldTSConfig = [];
123 if (isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.'])
124 && is_array($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.'])
125 ) {
126 $fieldTSConfig = $result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.'];
127 }
128 if (!empty($fieldTSConfig['label'])) {
129 $result['processedTca']['columns'][$fieldName]['label'] = $fieldTSConfig['label'];
130 }
131 if (!empty($fieldTSConfig['label.'][$languageService->lang])) {
132 $result['processedTca']['columns'][$fieldName]['label'] = $fieldTSConfig['label.'][$languageService->lang];
133 }
134 }
135 return $result;
136 }
137
138 /**
139 * Translate all labels if needed.
140 *
141 * @param array $result Result array
142 * @return array Modified result array
143 */
144 protected function translateLabels(array $result)
145 {
146 $languageService = $this->getLanguageService();
147 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfiguration) {
148 if (!isset($fieldConfiguration['label'])) {
149 continue;
150 }
151 $result['processedTca']['columns'][$fieldName]['label'] = $languageService->sL($fieldConfiguration['label']);
152 }
153 return $result;
154 }
155
156 /**
157 * @return LanguageService
158 */
159 protected function getLanguageService()
160 {
161 return $GLOBALS['LANG'];
162 }
163 }