[BUGFIX] FormEngine: Fix placeholder traversal for select type relations
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / TcaInputPlaceholders.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\FormDataCompiler;
18 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaInputPlaceholderRecord;
19 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\StringUtility;
23 use TYPO3\CMS\Lang\LanguageService;
24
25 /**
26 * Resolve placeholders for fields of type input or text. The placeholder value
27 * in the processedTca section of the result will be replaced with the resolved
28 * value.
29 */
30 class TcaInputPlaceholders implements FormDataProviderInterface
31 {
32 /**
33 * Resolve placeholders for input/text fields. Placeholders that are simple
34 * strings will be returned unmodified. Placeholders beginning with __row are
35 * being resolved, possibly traversing multiple tables.
36 *
37 * @param array $result
38 * @return array
39 */
40 public function addData(array $result)
41 {
42 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
43 // Placeholders are only valid for input and text type fields
44 if (
45 ($fieldConfig['config']['type'] !== 'input' && $fieldConfig['config']['type'] !== 'text')
46 || !isset($fieldConfig['config']['placeholder'])
47 ) {
48 continue;
49 }
50
51 // Resolve __row|field type placeholders
52 if (StringUtility::beginsWith($fieldConfig['config']['placeholder'], '__row|')) {
53 // split field names into array and remove the __row indicator
54 $fieldNameArray = array_slice(
55 GeneralUtility::trimExplode('|', $fieldConfig['config']['placeholder'], true),
56 1
57 );
58 $result['processedTca']['columns'][$fieldName]['config']['placeholder'] = $this->getPlaceholderValue($fieldNameArray, $result);
59 }
60
61 // Resolve placeholders from language files
62 if (StringUtility::beginsWith($fieldConfig['config']['placeholder'], 'LLL:')) {
63 $result['processedTca']['columns'][$fieldName]['config']['placeholder'] = $this->getLanguageService()->sl($fieldConfig['config']['placeholder']);
64 }
65
66 // Remove empty placeholders
67 if (empty($result['processedTca']['columns'][$fieldName]['config']['placeholder'])) {
68 unset($result['processedTca']['columns'][$fieldName]['config']['placeholder']);
69 }
70 }
71
72 return $result;
73 }
74
75 /**
76 * Recursively resolve the placeholder value. A placeholder string with a
77 * syntax of __row|field1|field2|field3 will be recursively resolved to a
78 * final value.
79 *
80 * @param array $fieldNameArray
81 * @param array $result
82 * @param int $recursionLevel
83 * @return string
84 */
85 protected function getPlaceholderValue($fieldNameArray, $result, $recursionLevel = 0)
86 {
87 if ($recursionLevel > 99) {
88 // This should not happen, treat as misconfiguration
89 return '';
90 }
91
92 $fieldName = array_shift($fieldNameArray);
93 $fieldConfig = $result['processedTca']['columns'][$fieldName]['config'];
94
95 // Skip if a defined field was actually not present in the database row
96 // Using array_key_exists here, since NULL values are valid as well.
97 if (!array_key_exists($fieldName, $result['databaseRow'])) {
98 return '';
99 }
100
101 $value = $result['databaseRow'][$fieldName];
102
103 switch ($fieldConfig['type']) {
104 case 'select':
105 // The FormDataProviders already resolved the select items to an array of uids,
106 // filter out empty values that occur when no related record has been selected.
107 $possibleUids = array_filter($value);
108 $foreignTableName = $fieldConfig['foreign_table'];
109 break;
110 case 'group':
111 $possibleUids = $this->getRelatedGroupFieldUids($fieldConfig, $value);
112 $foreignTableName = $this->getAllowedTableForGroupField($fieldConfig);
113 break;
114 case 'inline':
115 $possibleUids = array_filter(GeneralUtility::trimExplode(',', $value, true));
116 $foreignTableName = $fieldConfig['foreign_table'];
117 break;
118 default:
119 $possibleUids = [];
120 $foreignTableName = '';
121 }
122
123 if (!empty($possibleUids) && !empty($fieldNameArray)) {
124 $relatedFormData = $this->getRelatedFormData($foreignTableName, $possibleUids[0], $fieldNameArray[0]);
125 $value = $this->getPlaceholderValue($fieldNameArray, $relatedFormData, $recursionLevel + 1);
126 }
127
128 if ($recursionLevel === 0 && is_array($value)) {
129 $value = implode(', ', $value);
130 }
131 return (string)$value;
132 }
133
134 /**
135 * Compile a formdata result set based on the tablename and record uid.
136 *
137 * @param string $tableName Name of the table for which to compile formdata
138 * @param int $uid UID of the record for which to compile the formdata
139 * @param string $columnToProcess The column that is required from the record
140 * @return array The compiled formdata
141 */
142 protected function getRelatedFormData($tableName, $uid, $columnToProcess)
143 {
144 $fakeDataInput = [
145 'command' => 'edit',
146 'vanillaUid' => (int)$uid,
147 'tableName' => $tableName,
148 'inlineCompileExistingChildren' => false,
149 'columnsToProcess' => [$columnToProcess],
150 ];
151 /** @var TcaInputPlaceholderRecord $formDataGroup */
152 $formDataGroup = GeneralUtility::makeInstance(TcaInputPlaceholderRecord::class);
153 /** @var FormDataCompiler $formDataCompiler */
154 $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
155 $compilerResult = $formDataCompiler->compile($fakeDataInput);
156 return $compilerResult;
157 }
158
159 /**
160 * Return uids of related records for group type fields. Uids consisting of
161 * multiple parts like [table]_[uid]|[title] will be reduced to integers and
162 * validated against the allowed table. Uids without a table prefix are
163 * accepted in any case.
164 *
165 * @param array $fieldConfig TCA "config" section for the group type field.
166 * @param string $value A comma separated list of records
167 * @return array
168 */
169 protected function getRelatedGroupFieldUids(array $fieldConfig, $value)
170 {
171 $relatedUids = [];
172 $allowedTable = $this->getAllowedTableForGroupField($fieldConfig);
173
174 // Skip if it's not a database relation with a resolvable foreign table
175 if (($fieldConfig['internal_type'] !== 'db') || ($allowedTable === false)) {
176 return $relatedUids;
177 }
178
179 $values = GeneralUtility::trimExplode(',', $value, true);
180 foreach ($values as $groupValue) {
181 list($foreignIdentifier, $_) = GeneralUtility::trimExplode('|', $groupValue);
182 list($recordForeignTable, $foreignUid) = BackendUtility::splitTable_Uid($foreignIdentifier);
183 // skip records that do not match the allowed table
184 if (!empty($recordForeignTable) && ($recordForeignTable !== $allowedTable)) {
185 continue;
186 }
187 $relatedUids[] = $foreignUid;
188 }
189
190 return $relatedUids;
191 }
192
193 /**
194 * Will read the "allowed" value from the given field configuration
195 * and returns FALSE if none or more than one has been defined.
196 * Otherwise the name of the allowed table will be returned.
197 *
198 * @param array $fieldConfig TCA "config" section for the group type field.
199 * @return bool|string
200 */
201 protected function getAllowedTableForGroupField(array $fieldConfig)
202 {
203 $allowedTable = false;
204
205 $allowedTables = GeneralUtility::trimExplode(',', $fieldConfig['allowed'], true);
206 if (count($allowedTables) === 1) {
207 $allowedTable = $allowedTables[0];
208 }
209
210 return $allowedTable;
211 }
212
213 /**
214 * @return LanguageService
215 */
216 protected function getLanguageService()
217 {
218 return $GLOBALS['LANG'];
219 }
220 }