‪TYPO3CMS  ‪main
TcaJson.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
26 {
27  public function ‪addData(array $result): array
28  {
29  // Currently only new records are considered
30  if ($result['command'] !== 'new') {
31  return $result;
32  }
33 
34  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
35  if (($fieldConfig['config']['type'] ?? '') !== 'json') {
36  continue;
37  }
38 
39  // Ensure that even for new records, the field is always an array - especially if a default value is defined
40  if (is_string($result['databaseRow'][$fieldName])) {
41  try {
42  $result['databaseRow'][$fieldName] = json_decode($result['databaseRow'][$fieldName], true, 512, JSON_THROW_ON_ERROR);
43  } catch (\JsonException) {
44  $result['databaseRow'][$fieldName] = [];
45  }
46  }
47  }
48 
49  return $result;
50  }
51 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractDatabaseRecordProvider
Definition: AbstractDatabaseRecordProvider.php:31
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaJson
Definition: TcaJson.php:26
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaJson\addData
‪addData(array $result)
Definition: TcaJson.php:27
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23