[BUGFIX] Outdated resource pointers crash for TCA type group
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / TcaGroup.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\Database\RelationHandler;
19 use TYPO3\CMS\Core\Resource\Exception;
20 use TYPO3\CMS\Core\Resource\Folder;
21 use TYPO3\CMS\Core\Resource\ResourceFactory;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\PathUtility;
24
25 /**
26 * Resolve databaseRow field content to the real connected rows for type=group
27 */
28 class TcaGroup implements FormDataProviderInterface
29 {
30 /**
31 * Initialize new row with default values from various sources
32 *
33 * @param array $result
34 * @return array
35 * @todo: Should not implode valid values with | again, container & elements should work
36 * @todo: with the array as it was done for select items
37 * @throws \UnexpectedValueException
38 */
39 public function addData(array $result)
40 {
41 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
42 if (empty($fieldConfig['config']['type'])
43 || $fieldConfig['config']['type'] !== 'group'
44 || empty($fieldConfig['config']['internal_type'])
45 ) {
46 continue;
47 }
48
49 $databaseRowFieldContent = '';
50 if (!empty($result['databaseRow'][$fieldName])) {
51 $databaseRowFieldContent = (string)$result['databaseRow'][$fieldName];
52 }
53
54 $internalType = $fieldConfig['config']['internal_type'];
55 if ($internalType === 'file_reference' || $internalType === 'file') {
56 $files = [];
57 // Simple list of files
58 $fileList = GeneralUtility::trimExplode(',', $databaseRowFieldContent, true);
59 foreach ($fileList as $file) {
60 if ($file) {
61 $files[] = rawurlencode($file) . '|' . rawurlencode(PathUtility::basename($file));
62 }
63 }
64 $result['databaseRow'][$fieldName] = implode(',', $files);
65 } elseif ($internalType === 'db') {
66 /** @var $relationHandler RelationHandler */
67 $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
68 $relationHandler->start(
69 $databaseRowFieldContent,
70 $fieldConfig['config']['allowed'],
71 $fieldConfig['config']['MM'],
72 $result['databaseRow']['uid'],
73 $result['tableName'],
74 $fieldConfig['config']
75 );
76 $relationHandler->getFromDB();
77 $result['databaseRow'][$fieldName] = $relationHandler->readyForInterface();
78 } elseif ($internalType === 'folder') {
79 $folders = [];
80 // Simple list of folders
81 $folderList = GeneralUtility::trimExplode(',', $databaseRowFieldContent, true);
82 foreach ($folderList as $folder) {
83 if (empty($folder)) {
84 continue;
85 }
86 try {
87 $folderObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($folder);
88 if ($folderObject instanceof Folder) {
89 $folderName = PathUtility::basename($folderObject->getIdentifier());
90 $folders[] = rawurlencode($folder) . '|' . rawurlencode($folderName);
91 }
92 } catch (Exception $exception) {
93 continue;
94 }
95 }
96 $result['databaseRow'][$fieldName] = implode(',', $folders);
97 } else {
98 throw new \UnexpectedValueException(
99 'TCA internal_type of field "' . $fieldName . '" in table ' . $result['tableName']
100 . ' must be set to either "db", "file" or "file_reference"',
101 1438780511
102 );
103 }
104 }
105
106 return $result;
107 }
108 }