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