TYPO3  7.6
TcaFlexPrepare.php
Go to the documentation of this file.
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 
20 
27 {
37  public function addData(array $result)
38  {
39  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
40  if (empty($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'flex') {
41  continue;
42  }
43  $result = $this->createDefaultSheetInDataStructureIfNotGiven($result, $fieldName);
44  $result = $this->removeTceFormsArrayKeyFromDataStructureElements($result, $fieldName);
45  $result = $this->migrateFlexformTcaDataStructureElements($result, $fieldName);
46  }
47 
48  return $result;
49  }
50 
63  protected function createDefaultSheetInDataStructureIfNotGiven(array $result, $fieldName)
64  {
65  $modifiedDataStructure = $result['processedTca']['columns'][$fieldName]['config']['ds'];
66  if (isset($modifiedDataStructure['ROOT']) && isset($modifiedDataStructure['sheets'])) {
67  throw new \UnexpectedValueException(
68  'Parsed data structure has both ROOT and sheets on top level',
69  1440676540
70  );
71  }
72  if (isset($modifiedDataStructure['ROOT']) && is_array($modifiedDataStructure['ROOT'])) {
73  $modifiedDataStructure['sheets']['sDEF']['ROOT'] = $modifiedDataStructure['ROOT'];
74  unset($modifiedDataStructure['ROOT']);
75  }
76  $result['processedTca']['columns'][$fieldName]['config']['ds'] = $modifiedDataStructure;
77  return $result;
78  }
79 
91  protected function removeTceFormsArrayKeyFromDataStructureElements(array $result, $fieldName)
92  {
93  $modifiedDataStructure = $result['processedTca']['columns'][$fieldName]['config']['ds'];
94  $modifiedDataStructure = $this->removeElementTceFormsRecursive($modifiedDataStructure);
95  $result['processedTca']['columns'][$fieldName]['config']['ds'] = $modifiedDataStructure;
96  return $result;
97  }
98 
105  protected function removeElementTceFormsRecursive(array $structure)
106  {
107  $newStructure = [];
108  foreach ($structure as $key => $value) {
109  if ($key === 'ROOT' && is_array($value) && isset($value['TCEforms'])) {
110  $value = array_merge($value, $value['TCEforms']);
111  unset($value['TCEforms']);
112  }
113  if ($key === 'el' && is_array($value)) {
114  $newSubStructure = [];
115  foreach ($value as $subKey => $subValue) {
116  if (is_array($subValue) && count($subValue) === 1 && isset($subValue['TCEforms'])) {
117  $newSubStructure[$subKey] = $subValue['TCEforms'];
118  } else {
119  $newSubStructure[$subKey] = $subValue;
120  }
121  }
122  $value = $newSubStructure;
123  }
124  if (is_array($value)) {
125  $value = $this->removeElementTceFormsRecursive($value);
126  }
127  $newStructure[$key] = $value;
128  }
129  return $newStructure;
130  }
131 
139  protected function migrateFlexformTcaDataStructureElements(array $result, $fieldName)
140  {
141  $modifiedDataStructure = $result['processedTca']['columns'][$fieldName]['config']['ds'];
142  $modifiedDataStructure = $this->migrateFlexformTcaRecursive($modifiedDataStructure, $result['tableName'], $fieldName);
143  $result['processedTca']['columns'][$fieldName]['config']['ds'] = $modifiedDataStructure;
144  return $result;
145  }
146 
155  protected function migrateFlexformTcaRecursive($structure, $table, $fieldName)
156  {
157  $newStructure = [];
158  foreach ($structure as $key => $value) {
159  if ($key === 'el' && is_array($value)) {
160  $newSubStructure = [];
161  $tcaMigration = GeneralUtility::makeInstance(TcaMigration::class);
162  foreach ($value as $subKey => $subValue) {
163  // On-the-fly migration for flex form "TCA"
164  // @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8. This can be removed *if* no additional TCA migration is added with CMS 8, see class TcaMigration
165  $dummyTca = array(
166  'dummyTable' => array(
167  'columns' => array(
168  'dummyField' => $subValue,
169  ),
170  ),
171  );
172  $migratedTca = $tcaMigration->migrate($dummyTca);
173  $messages = $tcaMigration->getMessages();
174  if (!empty($messages)) {
175  $context = 'FormEngine did an on-the-fly migration of a flex form data structure. This is deprecated and will be removed'
176  . ' with TYPO3 CMS 8. Merge the following changes into the flex form definition of table "' . $table . '"" in field "' . $fieldName . '"":';
177  array_unshift($messages, $context);
178  GeneralUtility::deprecationLog(implode(LF, $messages));
179  }
180  $newSubStructure[$subKey] = $migratedTca['dummyTable']['columns']['dummyField'];
181  }
182  $value = $newSubStructure;
183  }
184  if (is_array($value)) {
185  $value = $this->migrateFlexformTcaRecursive($value, $table, $fieldName);
186  }
187  $newStructure[$key] = $value;
188  }
189  return $newStructure;
190  }
191 }