[CLEANUP] Minor code improvements in DatabaseRowInitializeNew
[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 * There are 4 sources of default values. Mind the order, the last takes precedence.
28 *
29 * @param array $result
30 * @return array
31 */
32 public function addData(array $result) {
33 if ($result['command'] !== 'new') {
34 return $result;
35 }
36
37 $databaseRow = array();
38
39 $tableName = $result['tableName'];
40 $tableNameWithDot = $tableName . '.';
41
42 // Apply default values from user typo script "TCAdefaults" if any
43 if (isset($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
44 && is_array($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
45 ) {
46 foreach ($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
47 if (isset($result['vanillaTableTca']['columns'][$fieldName])) {
48 $databaseRow[$fieldName] = $fieldValue;
49 }
50 }
51 }
52
53 // Apply defaults from pageTsConfig
54 if (isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
55 && is_array($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
56 ) {
57 foreach ($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
58 if (isset($result['vanillaTableTca']['columns'][$fieldName])) {
59 $databaseRow[$fieldName] = $fieldValue;
60 }
61 }
62 }
63
64 // If a neighbor row is given (if vanillaUid was negative), field can be initialized with values
65 // from neighbor for fields registered in TCA['ctrl']['useColumnsForDefaultValues'].
66 if (is_array($result['neighborRow'])
67 && !empty($result['vanillaTableTca']['ctrl']['useColumnsForDefaultValues'])
68 ) {
69 $defaultColumns = GeneralUtility::trimExplode(',', $result['vanillaTableTca']['ctrl']['useColumnsForDefaultValues'], TRUE);
70 foreach ($defaultColumns as $fieldName) {
71 if (isset($result['vanillaTableTca']['columns'][$fieldName])
72 && isset($result['neighborRow'][$fieldName])
73 ) {
74 $databaseRow[$fieldName] = $result['neighborRow'][$fieldName];
75 }
76 }
77 }
78
79 // Apply default values from GET / POST
80 // @todo: Fetch this stuff from request object as soon as modules were moved to PSR-7
81 $defaultValuesFromGetPost = GeneralUtility::_GP('defVals');
82 if (isset($defaultValuesFromGetPost[$tableName])
83 && is_array($defaultValuesFromGetPost[$tableName])
84 ) {
85 foreach ($defaultValuesFromGetPost[$tableName] as $fieldName => $fieldValue) {
86 if (isset($result['vanillaTableTca']['columns'][$fieldName])) {
87 $databaseRow[$fieldName] = $fieldValue;
88 }
89 }
90 }
91
92 // Set pid to vanillaUid. This means, it *can* be negative, if the record is added relative to another record
93 $databaseRow['pid'] = $result['vanillaUid'];
94
95 $result['databaseRow'] = $databaseRow;
96 return $result;
97 }
98
99 }