‪TYPO3CMS  ‪main
TypeFlex.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 
25 
32 {
36  protected ‪$matchArray = [
37  'fieldConfig' => [
38  'config' => [
39  'type' => 'flex',
40  ],
41  ],
42  ];
43 
50  public function ‪generate(array $data): string
51  {
52  // Parse the flex form
53  $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
54  $structureIdentifier = $flexFormTools->getDataStructureIdentifier($data['fieldConfig'], $data['tableName'], $data['fieldName'], []);
55  $dataStructureArray = $flexFormTools->parseDataStructureByIdentifier($structureIdentifier);
56 
57  // Early return if flex couldn't be parsed
58  if (!is_array($dataStructureArray)) {
59  return '';
60  }
61 
62  // Loop through this xml mess and call a generator for each found field
63  $aFlexFieldData = $data;
64  $resultArray = [];
66  $resolver = GeneralUtility::makeInstance(FieldGeneratorResolver::class);
67  if (isset($dataStructureArray['sheets']) && is_array($dataStructureArray['sheets'])) {
68  foreach ($dataStructureArray['sheets'] as $sheetName => $sheetArray) {
69  if (isset($sheetArray['ROOT']['el']) && is_array($sheetArray['ROOT']['el'])) {
70  foreach ($sheetArray['ROOT']['el'] as $sheetElementName => $sheetElementArray) {
71  // Container section
72  if (isset($sheetElementArray['type']) && $sheetElementArray['type'] === 'array'
73  && isset($sheetElementArray['section']) && $sheetElementArray['section'] == 1
74  && isset($sheetElementArray['el']) && is_array($sheetElementArray['el'])
75  ) {
76  $containerCounter = 0;
77  foreach ($sheetElementArray['el'] as $containerName => $containerElementArray) {
78  if (!isset($containerElementArray['el']) || !is_array($containerElementArray['el'])) {
79  continue;
80  }
81  $containerCounter++;
82  foreach ($containerElementArray['el'] as $containerSingleElementName => $containerSingleElementArray) {
83  $aFlexFieldData['fieldName'] = $containerSingleElementName;
84  $aFlexFieldData['fieldConfig'] = $containerSingleElementArray;
85  try {
86  $generator = $resolver->resolve($aFlexFieldData);
87  $flexFieldValue = $generator->generate($aFlexFieldData);
88  $resultArray['data'][$sheetName]['lDEF']
89  [$sheetElementName]['el']
90  [$containerCounter][$containerName]['el']
91  [$containerSingleElementName]['vDEF'] = $flexFieldValue;
92  } catch (‪GeneratorNotFoundException $e) {
93  // No op if no matching generator was found
94  }
95  // Field handled, skip rest
96  continue;
97  }
98  }
99  } else {
100  // Casual field
101  $aFlexFieldData['fieldName'] = $sheetElementName;
102  $aFlexFieldData['fieldConfig'] = $sheetElementArray;
103  try {
104  $generator = $resolver->resolve($aFlexFieldData);
105  $flexFieldValue = $generator->generate($aFlexFieldData);
106  $resultArray['data'][$sheetName]['lDEF'][$sheetElementName]['vDEF'] = $flexFieldValue;
107  } catch (‪GeneratorNotFoundException $e) {
108  // No op if no matching generator was found
109  }
110  }
111  }
112  }
113  }
114  } elseif (isset($dataStructureArray['ROOT']['el']) && is_array($dataStructureArray['ROOT']['el'])) {
115  foreach ($dataStructureArray['ROOT']['el'] as $elementName => $elementArray) {
116  $aFlexFieldData['fieldName'] = $elementName;
117  $aFlexFieldData['fieldConfig'] = $elementArray;
118  try {
119  $generator = $resolver->resolve($aFlexFieldData);
120  $flexFieldValue = $generator->generate($aFlexFieldData);
121  $resultArray['data']['sDEF']['lDEF'][$elementName]['vDEF'] = $flexFieldValue;
122  } catch (‪GeneratorNotFoundException $e) {
123  // No op if no matching generator was found
124  }
125  }
126  }
127 
128  // Get string representation of result via FlexFormTools
129  $resultString = '';
130  if (!empty($resultArray)) {
131  $resultString = $flexFormTools->flexArray2Xml($resultArray);
132  }
133 
134  return $resultString;
135  }
136 }
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator\TypeFlex
Definition: TypeFlex.php:32
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator\TypeFlex\$matchArray
‪array $matchArray
Definition: TypeFlex.php:35
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator\AbstractFieldGenerator
Definition: AbstractFieldGenerator.php:29
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGeneratorResolver
Definition: FieldGeneratorResolver.php:28
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator\TypeFlex\generate
‪string generate(array $data)
Definition: TypeFlex.php:49
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:40
‪TYPO3\CMS\Styleguide\TcaDataGenerator\GeneratorNotFoundException
Definition: GeneratorNotFoundException.php:25
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGenerator
Definition: AbstractFieldGenerator.php:18
‪TYPO3\CMS\Styleguide\TcaDataGenerator\FieldGeneratorInterface
Definition: FieldGeneratorInterface.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52