‪TYPO3CMS  11.5
TcaFlexPrepare.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 
28 
35 {
45  public function ‪addData(array $result)
46  {
47  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
48  if (empty($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'flex') {
49  continue;
50  }
51  $result = $this->‪initializeDataStructure($result, $fieldName);
52  $result = $this->‪initializeDataValues($result, $fieldName);
53  $result = $this->‪removeTceFormsArrayKeyFromDataStructureElements($result, $fieldName);
54  $result = $this->‪migrateFlexformTcaDataStructureElements($result, $fieldName);
55  }
56 
57  return $result;
58  }
59 
71  protected function ‪initializeDataStructure(array $result, $fieldName)
72  {
73  if (!isset($result['processedTca']['columns'][$fieldName]['config']['dataStructureIdentifier'])) {
74  $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
75 
76  $dataStructureArray = ['sheets' => ['sDEF' => []]];
77  $dataStructureIdentifier = null;
78 
79  try {
80  $dataStructureIdentifier = $flexFormTools->getDataStructureIdentifier(
81  $result['processedTca']['columns'][$fieldName],
82  $result['tableName'],
83  $fieldName,
84  $result['databaseRow']
85  );
86  $dataStructureArray = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
88  $dataStructureIdentifier = null;
89  } finally {
90  // Add the identifier to TCA to use it later during rendering
91  $result['processedTca']['columns'][$fieldName]['config']['dataStructureIdentifier'] = $dataStructureIdentifier;
92  }
93  } else {
94  // Assume the data structure has been given from outside if the data structure identifier is already set.
95  $dataStructureArray = $result['processedTca']['columns'][$fieldName]['config']['ds'];
96  }
97  if (!isset($dataStructureArray['meta']) || !is_array($dataStructureArray['meta'])) {
98  $dataStructureArray['meta'] = [];
99  }
100  // This kicks one array depth: config['ds']['listOfDataStructures'] becomes config['ds']
101  // This also ensures the final ds can be found in 'ds', even if the DS was fetch from
102  // a record, see FlexFormTools->getDataStructureIdentifier() for details.
103  $result['processedTca']['columns'][$fieldName]['config']['ds'] = $dataStructureArray;
104  return $result;
105  }
106 
114  protected function ‪initializeDataValues(array $result, $fieldName)
115  {
116  if (!array_key_exists($fieldName, $result['databaseRow'])) {
117  $result['databaseRow'][$fieldName] = '';
118  }
119  $valueArray = [];
120  if (isset($result['databaseRow'][$fieldName])) {
121  $valueArray = $result['databaseRow'][$fieldName];
122  }
123  if (!is_array($result['databaseRow'][$fieldName])) {
124  $valueArray = ‪GeneralUtility::xml2array($result['databaseRow'][$fieldName]);
125  }
126  if (!is_array($valueArray)) {
127  $valueArray = [];
128  }
129  if (!isset($valueArray['data'])) {
130  $valueArray['data'] = [];
131  }
132  if (!isset($valueArray['meta'])) {
133  $valueArray['meta'] = [];
134  }
135  $result['databaseRow'][$fieldName] = $valueArray;
136  return $result;
137  }
138 
150  protected function ‪removeTceFormsArrayKeyFromDataStructureElements(array $result, $fieldName)
151  {
152  $modifiedDataStructure = $result['processedTca']['columns'][$fieldName]['config']['ds'];
153  $modifiedDataStructure = $this->‪removeElementTceFormsRecursive($modifiedDataStructure);
154  $result['processedTca']['columns'][$fieldName]['config']['ds'] = $modifiedDataStructure;
155  return $result;
156  }
157 
164  protected function ‪removeElementTceFormsRecursive(array $structure)
165  {
166  $newStructure = [];
167  foreach ($structure as $key => $value) {
168  if ($key === 'ROOT' && is_array($value) && isset($value['TCEforms'])) {
169  $value = array_merge($value, $value['TCEforms']);
170  unset($value['TCEforms']);
171  }
172  if ($key === 'el' && is_array($value)) {
173  $newSubStructure = [];
174  foreach ($value as $subKey => $subValue) {
175  if (is_array($subValue) && count($subValue) === 1 && isset($subValue['TCEforms'])) {
176  $newSubStructure[$subKey] = $subValue['TCEforms'];
177  } else {
178  $newSubStructure[$subKey] = $subValue;
179  }
180  }
181  $value = $newSubStructure;
182  }
183  if (is_array($value)) {
184  $value = $this->‪removeElementTceFormsRecursive($value);
185  }
186  $newStructure[$key] = $value;
187  }
188  return $newStructure;
189  }
190 
198  protected function ‪migrateFlexformTcaDataStructureElements(array $result, $fieldName)
199  {
200  $modifiedDataStructure = $result['processedTca']['columns'][$fieldName]['config']['ds'];
201  $modifiedDataStructure = $this->‪migrateFlexformTcaRecursive($modifiedDataStructure, $result['tableName'], $fieldName);
202  $result['processedTca']['columns'][$fieldName]['config']['ds'] = $modifiedDataStructure;
203  return $result;
204  }
205 
214  protected function ‪migrateFlexformTcaRecursive($structure, $table, $fieldName)
215  {
216  $newStructure = [];
217  foreach ($structure as $key => $value) {
218  if ($key === 'el' && is_array($value)) {
219  $newSubStructure = [];
220  $tcaMigration = GeneralUtility::makeInstance(TcaMigration::class);
221  $tcaPreparation = GeneralUtility::makeInstance(TcaPreparation::class);
222  foreach ($value as $subKey => $subValue) {
223  // On-the-fly migration for flex form "TCA". Call the TcaMigration and log any deprecations.
224  $dummyTca = [
225  'dummyTable' => [
226  'columns' => [
227  'dummyField' => $subValue,
228  ],
229  ],
230  ];
231  $migratedTca = $tcaMigration->migrate($dummyTca);
232  $messages = $tcaMigration->getMessages();
233  if (!empty($messages)) {
234  $context = 'FormEngine did an on-the-fly migration of a flex form data structure. This is deprecated and will be removed.'
235  . ' Merge the following changes into the flex form definition of table "' . $table . '"" in field "' . $fieldName . '"":';
236  array_unshift($messages, $context);
237  trigger_error(implode(LF, $messages), E_USER_DEPRECATED);
238  }
239  $preparedTca = $tcaPreparation->prepare($migratedTca);
240  $newSubStructure[$subKey] = $preparedTca['dummyTable']['columns']['dummyField'];
241  }
242  $value = $newSubStructure;
243  }
244  if (is_array($value)) {
245  $value = $this->‪migrateFlexformTcaRecursive($value, $table, $fieldName);
246  }
247  $newStructure[$key] = $value;
248  }
249  return $newStructure;
250  }
251 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\xml2array
‪static mixed xml2array($string, $NSprefix='', $reportDocTag=false)
Definition: GeneralUtility.php:1482
‪TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowException
Definition: InvalidParentRowException.php:22
‪TYPO3\CMS\Core\Migrations\TcaMigration
Definition: TcaMigration.php:26
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare\initializeDataStructure
‪array initializeDataStructure(array $result, $fieldName)
Definition: TcaFlexPrepare.php:71
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare
Definition: TcaFlexPrepare.php:35
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare\removeTceFormsArrayKeyFromDataStructureElements
‪array removeTceFormsArrayKeyFromDataStructureElements(array $result, $fieldName)
Definition: TcaFlexPrepare.php:150
‪TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowRootException
Definition: InvalidParentRowRootException.php:21
‪TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidIdentifierException
Definition: InvalidIdentifierException.php:21
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:40
‪TYPO3\CMS\Core\Preparations\TcaPreparation
Definition: TcaPreparation.php:31
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare\migrateFlexformTcaDataStructureElements
‪array migrateFlexformTcaDataStructureElements(array $result, $fieldName)
Definition: TcaFlexPrepare.php:198
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare\removeElementTceFormsRecursive
‪array removeElementTceFormsRecursive(array $structure)
Definition: TcaFlexPrepare.php:164
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23
‪TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidPointerFieldValueException
Definition: InvalidPointerFieldValueException.php:21
‪TYPO3\CMS\Core\Configuration\FlexForm\Exception\InvalidParentRowLoopException
Definition: InvalidParentRowLoopException.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare\addData
‪array addData(array $result)
Definition: TcaFlexPrepare.php:45
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare\initializeDataValues
‪array initializeDataValues(array $result, $fieldName)
Definition: TcaFlexPrepare.php:114
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare\migrateFlexformTcaRecursive
‪array migrateFlexformTcaRecursive($structure, $table, $fieldName)
Definition: TcaFlexPrepare.php:214