‪TYPO3CMS  9.5
TcaGroup.php
Go to the documentation of this file.
1 <?php
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 
26 
31 {
40  public function ‪addData(array $result)
41  {
42  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
43  if (empty($fieldConfig['config']['type'])
44  || $fieldConfig['config']['type'] !== 'group'
45  || empty($fieldConfig['config']['internal_type'])
46  ) {
47  continue;
48  }
49 
50  // Sanitize max items, set to 99999 if not defined
51  $result['processedTca']['columns'][$fieldName]['config']['maxitems'] = ‪MathUtility::forceIntegerInRange(
52  $fieldConfig['config']['maxitems'] ?? 0,
53  0,
54  99999
55  );
56  if ($result['processedTca']['columns'][$fieldName]['config']['maxitems'] === 0) {
57  $result['processedTca']['columns'][$fieldName]['config']['maxitems'] = 99999;
58  }
59 
60  $databaseRowFieldContent = '';
61  if (!empty($result['databaseRow'][$fieldName])) {
62  $databaseRowFieldContent = (string)$result['databaseRow'][$fieldName];
63  }
64 
65  $items = [];
66  $sanitizedClipboardElements = [];
67  $internalType = $fieldConfig['config']['internal_type'];
68 
69  if ($internalType === 'file_reference' || $internalType === 'file') {
70  // @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0. Deprecation logged by TcaMigration class.
71  // Set 'allowed' config to "*" if not set
72  if (empty($fieldConfig['config']['allowed'])) {
73  $result['processedTca']['columns'][$fieldName]['config']['allowed'] = '*';
74  }
75  // Force empty uploadfolder for file_reference type
76  if ($internalType === 'file_reference') {
77  $result['processedTca']['columns'][$fieldName]['config']['uploadfolder'] = '';
78  }
79 
80  // Simple list of files
81  $fileList = GeneralUtility::trimExplode(',', $databaseRowFieldContent, true);
82  $fileFactory = ‪ResourceFactory::getInstance();
83  foreach ($fileList as $uidOrPath) {
84  $item = [
85  'uidOrPath' => $uidOrPath,
86  'title' => $uidOrPath,
87  ];
88  try {
90  $fileObject = $fileFactory->getFileObject($uidOrPath);
91  $item['title'] = $fileObject->getName();
92  }
93  } catch (‪Exception $exception) {
94  continue;
95  }
96  $items[] = $item;
97  }
98 
99  // Register elements from clipboard
100  $allowed = GeneralUtility::trimExplode(',', $result['processedTca']['columns'][$fieldName]['config']['allowed'], true);
101  $clipboard = GeneralUtility::makeInstance(Clipboard::class);
102  $clipboard->initializeClipboard();
103  $clipboardElements = $clipboard->elFromTable('_FILE');
104  if ($allowed[0] !== '*') {
105  // If there are a set of allowed extensions, filter the content
106  foreach ($clipboardElements as $elementValue) {
107  $pathInfo = pathinfo($elementValue);
108  if (in_array(strtolower($pathInfo['extension']), $allowed)) {
109  $sanitizedClipboardElements[] = [
110  'title' => $elementValue,
111  'value' => $elementValue,
112  ];
113  }
114  }
115  } else {
116  // If all is allowed, insert all. This does NOT respect any disallowed extensions,
117  // but those will be filtered away by the DataHandler
118  foreach ($clipboardElements as $elementValue) {
119  $sanitizedClipboardElements[] = [
120  'title' => $elementValue,
121  'value' => $elementValue,
122  ];
123  }
124  }
125  } elseif ($internalType === 'db') {
126  if (empty($fieldConfig['config']['allowed'])) {
127  throw new \RuntimeException(
128  'Mandatory TCA config setting "allowed" missing in field "' . $fieldName . '" of table "' . $result['tableName'] . '"',
129  1482250512
130  );
131  }
132 
133  // In case of vanilla uid, 0 is used to query relations by splitting $databaseRowFieldContent (possible defVals)
134  $MMuid = ‪MathUtility::canBeInterpretedAsInteger($result['databaseRow']['uid']) ? $result['databaseRow']['uid'] : 0;
135 
136  $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
137  $relationHandler->start(
138  $databaseRowFieldContent,
139  $fieldConfig['config']['allowed'],
140  $fieldConfig['config']['MM'],
141  $MMuid,
142  $result['tableName'],
143  $fieldConfig['config']
144  );
145  $relationHandler->getFromDB();
146  $relations = $relationHandler->getResolvedItemArray();
147  foreach ($relations as $relation) {
148  $tableName = $relation['table'];
149  $uid = $relation['uid'];
150  $record = ‪BackendUtility::getRecordWSOL($tableName, $uid);
151  $title = ‪BackendUtility::getRecordTitle($tableName, $record, false, false);
152  $items[] = [
153  'table' => $tableName,
154  'uid' => $record['uid'] ?? null,
155  'title' => $title,
156  'row' => $record,
157  ];
158  }
159 
160  // Register elements from clipboard
161  $allowed = GeneralUtility::trimExplode(',', $fieldConfig['config']['allowed'], true);
162  $clipboard = GeneralUtility::makeInstance(Clipboard::class);
163  $clipboard->initializeClipboard();
164  if ($allowed[0] !== '*') {
165  // Only some tables, filter them:
166  foreach ($allowed as $tablename) {
167  foreach ($clipboard->elFromTable($tablename) as $recordUid) {
168  $record = ‪BackendUtility::getRecordWSOL($tablename, $recordUid);
169  $sanitizedClipboardElements[] = [
170  'title' => ‪BackendUtility::getRecordTitle($tablename, $record),
171  'value' => $tablename . '_' . $recordUid,
172  ];
173  }
174  }
175  } else {
176  // All tables allowed for relation:
177  $clipboardElements = array_keys($clipboard->elFromTable(''));
178  foreach ($clipboardElements as $elementValue) {
179  list($elementTable, $elementUid) = explode('|', $elementValue);
180  $record = ‪BackendUtility::getRecordWSOL($elementTable, $elementUid);
181  $sanitizedClipboardElements[] = [
182  'title' => ‪BackendUtility::getRecordTitle($elementTable, $record),
183  'value' => $elementTable . '_' . $elementUid,
184  ];
185  }
186  }
187  } elseif ($internalType === 'folder') {
188  // Simple list of folders
189  $folderList = GeneralUtility::trimExplode(',', $databaseRowFieldContent, true);
190  foreach ($folderList as $folder) {
191  if (empty($folder)) {
192  continue;
193  }
194  try {
196  if ($folderObject instanceof ‪Folder) {
197  $items[] = [
198  'folder' => $folder,
199  ];
200  }
201  } catch (‪Exception $exception) {
202  continue;
203  }
204  }
205  } else {
206  throw new \UnexpectedValueException(
207  'TCA internal_type of field "' . $fieldName . '" in table ' . $result['tableName']
208  . ' must be set to either "db", "file" or "file_reference"',
209  1438780511
210  );
211  }
212 
213  $result['databaseRow'][$fieldName] = $items;
214  $result['processedTca']['columns'][$fieldName]['config']['clipboardElements'] = $sanitizedClipboardElements;
215  }
216 
217  return $result;
218  }
219 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup
Definition: TcaGroup.php:31
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Backend\Clipboard\Clipboard
Definition: Clipboard.php:38
‪TYPO3\CMS\Core\Database\RelationHandler
Definition: RelationHandler.php:32
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecordTitle
‪static string getRecordTitle($table, $row, $prep=false, $forceResult=true)
Definition: BackendUtility.php:1811
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:2
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecordWSOL
‪static array getRecordWSOL( $table, $uid, $fields=' *', $where='', $useDeleteClause=true, $unsetMovePointers=false)
Definition: BackendUtility.php:174
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:22
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup\addData
‪array addData(array $result)
Definition: TcaGroup.php:40
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Exception
Definition: AbstractFileOperationException.php:2
‪TYPO3\CMS\Core\Resource\ResourceFactory\retrieveFileOrFolderObject
‪File Folder null retrieveFileOrFolderObject($input)
Definition: ResourceFactory.php:491