4946c538f7b105f93818be983e47a4717b91bf5b
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / DatabaseRowDefaultValues.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 * Handle TCA default values on row. This affects existing rows as well as new rows.
22 *
23 * Hint: Even after this class it is NOT safe no rely that *all* fields from
24 * columns are set in databaseRow.
25 */
26 class DatabaseRowDefaultValues implements FormDataProviderInterface
27 {
28 /**
29 * Initialize new row with default values from various sources
30 *
31 * @param array $result
32 * @return array
33 */
34 public function addData(array $result)
35 {
36 $databaseRow = $result['databaseRow'];
37
38 $newRow = $databaseRow;
39 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
40 // Keep current value if it can be resolved to "the is something" directly
41 if (isset($databaseRow[$fieldName])) {
42 $newRow[$fieldName] = $databaseRow[$fieldName];
43 continue;
44 }
45
46 // Special handling for eval null
47 if (!empty($fieldConfig['config']['eval']) && GeneralUtility::inList($fieldConfig['config']['eval'], 'null')) {
48 if (// Field exists and is set to NULL
49 array_key_exists($fieldName, $databaseRow)
50 // Default NULL is set, and this is a new record!
51 || (array_key_exists('default', $fieldConfig['config']) && $fieldConfig['config']['default'] === null)
52 ) {
53 $newRow[$fieldName] = null;
54 } else {
55 $newRow[$fieldName] = (string)$fieldConfig['config']['default'];
56 }
57 } else {
58 // Fun part: This forces empty string for any field even if no default is set. Unsure if that is a good idea.
59 $newRow[$fieldName] = (string)$fieldConfig['config']['default'];
60 }
61 }
62
63 $result['databaseRow'] = $newRow;
64
65 return $result;
66 }
67 }