‪TYPO3CMS  ‪main
DatabaseRowDefaultValues.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 
27 {
33  public function ‪addData(array $result)
34  {
35  $databaseRow = $result['databaseRow'];
36 
37  $newRow = $databaseRow;
38  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
39  // Keep current value if it can be resolved to "there is something" directly
40  if (isset($databaseRow[$fieldName])) {
41  $newRow[$fieldName] = $databaseRow[$fieldName];
42  continue;
43  }
44 
45  // Special handling for nullable fields
46  if ($fieldConfig['config']['nullable'] ?? false) {
47  if (// Field exists and is set to NULL
48  array_key_exists($fieldName, $databaseRow)
49  // Default NULL is set, and this is a new record!
50  || (array_key_exists('default', $fieldConfig['config']) && $fieldConfig['config']['default'] === null)
51  ) {
52  $newRow[$fieldName] = null;
53  } else {
54  $newRow[$fieldName] = (string)($fieldConfig['config']['default'] ?? '');
55  }
56  } else {
57  // Fun part: This forces empty string for any field even if no default is set. This is
58  // a useful side effect in flex form section containers where a new field is added to an existing
59  // value array because it was added to a data structure.
60  $newRow[$fieldName] = (string)($fieldConfig['config']['default'] ?? '');
61  }
62  }
63 
64  $result['databaseRow'] = $newRow;
65 
66  return $result;
67  }
68 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowDefaultValues
Definition: DatabaseRowDefaultValues.php:27
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowDefaultValues\addData
‪array addData(array $result)
Definition: DatabaseRowDefaultValues.php:33
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23