a375f285dac882b7a9ddd3ddb737824faa683638
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / DatabaseRowInitializeNew.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\Utility\GeneralUtility;
19
20 /**
21 * On "new" command, initialize new database row with default data
22 */
23 class DatabaseRowInitializeNew implements FormDataProviderInterface {
24
25 /**
26 * Initialize new row with default values from various sources
27 *
28 * @param array $result
29 * @return array
30 */
31 public function addData(array $result) {
32 if ($result['command'] !== 'new') {
33 return $result;
34 }
35
36 $databaseRow = array();
37
38 $tableName = $result['tableName'];
39 $tableNameWithDot = $tableName . '.';
40
41 // Apply default values from user typo script "TCAdefaults" if any
42 if (isset($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
43 && is_array($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
44 ) {
45 foreach ($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
46 if (isset($result['vanillaTableTca']['columns'][$fieldName])) {
47 $databaseRow[$fieldName] = $fieldValue;
48 }
49 }
50 }
51
52 // Apply defaults from pageTsConfig
53 if (isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
54 && is_array($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
55 ) {
56 foreach ($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
57 if (isset($result['vanillaTableTca']['columns'][$fieldName])) {
58 $databaseRow[$fieldName] = $fieldValue;
59 }
60 }
61 }
62
63 // Apply default values from GET / POST
64 // @todo: Fetch this stuff from request object as soon as modules were moved to PSR-7
65 $defaultValuesFromGetPost = GeneralUtility::_GP('defVals');
66 if (isset($defaultValuesFromGetPost[$tableName])
67 && is_array($defaultValuesFromGetPost[$tableName])
68 ) {
69 foreach ($defaultValuesFromGetPost[$tableName] as $fieldName => $fieldValue) {
70 if (isset($result['vanillaTableTca']['columns'][$fieldName])) {
71 $databaseRow[$fieldName] = $fieldValue;
72 }
73 }
74 }
75
76 // If a neighbor row is given (if vanillaUid was negative), field can be initialized with values
77 // from neighbor for fields registered in TCA['ctrl']['useColumnsForDefaultValues'].
78 if (is_array($result['neighborRow'])
79 && isset($result['vanillaTableTca']['ctrl']['useColumnsForDefaultValues'])
80 && !empty($result['vanillaTableTca']['ctrl']['useColumnsForDefaultValues'])
81 ) {
82 $defaultColumns = GeneralUtility::trimExplode(',', $result['vanillaTableTca']['ctrl']['useColumnsForDefaultValues'], TRUE);
83 foreach ($defaultColumns as $fieldName) {
84 if (isset($result['vanillaTableTca']['columns'][$fieldName])
85 && isset($result['neighborRow'][$fieldName])
86 // Set only if not initialized by other default
87 // @todo: Why shouldn't neighbor override a different default, where is that useful?
88 && !isset($databaseRow[$fieldName])
89 ) {
90 $databaseRow[$fieldName] = $result['neighborRow'][$fieldName];
91 }
92 }
93 }
94
95 // Set pid to vanillaUid. This means, it *can* be negative, if the record is added relative to another record
96 $databaseRow['pid'] = $result['vanillaUid'];
97
98 $result['databaseRow'] = $databaseRow;
99 return $result;
100 }
101
102 }