TYPO3  7.6
TcaFlexFetch.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 {
34  public function addData(array $result)
35  {
36  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
37  if (empty($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'flex') {
38  continue;
39  }
40  $result = $this->initializeDataStructure($result, $fieldName);
41  $result = $this->initializeDataValues($result, $fieldName);
42  $result = $this->resolvePossibleExternalFileInDataStructure($result, $fieldName);
43  }
44 
45  return $result;
46  }
47 
59  protected function initializeDataStructure(array $result, $fieldName)
60  {
61  // Fetch / initialize data structure
62  $dataStructureArray = BackendUtility::getFlexFormDS(
63  $result['processedTca']['columns'][$fieldName]['config'],
64  $result['databaseRow'],
65  $result['tableName'],
66  $fieldName
67  );
68  // If data structure can't be parsed, this is a developer error, so throw a non catchable exception
69  if (!is_array($dataStructureArray)) {
70  throw new \UnexpectedValueException(
71  'Data structure error: ' . $dataStructureArray,
72  1440506893
73  );
74  }
75  if (!isset($dataStructureArray['meta']) || !is_array($dataStructureArray['meta'])) {
76  $dataStructureArray['meta'] = array();
77  }
78  // This kicks one array depth: config['ds']['matchingIdentifier'] becomes config['ds']
79  $result['processedTca']['columns'][$fieldName]['config']['ds'] = $dataStructureArray;
80  return $result;
81  }
82 
90  protected function initializeDataValues(array $result, $fieldName)
91  {
92  if (!array_key_exists($fieldName, $result['databaseRow'])) {
93  $result['databaseRow'][$fieldName] = '';
94  }
95  $valueArray = [];
96  if (isset($result['databaseRow'][$fieldName])) {
97  $valueArray = $result['databaseRow'][$fieldName];
98  }
99  if (!is_array($result['databaseRow'][$fieldName])) {
100  $valueArray = GeneralUtility::xml2array($result['databaseRow'][$fieldName]);
101  }
102  if (!is_array($valueArray)) {
103  $valueArray = [];
104  }
105  if (!isset($valueArray['data'])) {
106  $valueArray['data'] = [];
107  }
108  if (!isset($valueArray['meta'])) {
109  $valueArray['meta'] = [];
110  }
111  $result['databaseRow'][$fieldName] = $valueArray;
112  return $result;
113  }
114 
126  protected function resolvePossibleExternalFileInDataStructure(array $result, $fieldName)
127  {
128  $modifiedDataStructure = $result['processedTca']['columns'][$fieldName]['config']['ds'];
129  if (isset($modifiedDataStructure['sheets']) && is_array($modifiedDataStructure['sheets'])) {
130  foreach ($modifiedDataStructure['sheets'] as $sheetName => $sheetStructure) {
131  if (!is_array($sheetStructure)) {
132  $file = GeneralUtility::getFileAbsFileName($sheetStructure);
133  if ($file && @is_file($file)) {
134  $sheetStructure = GeneralUtility::xml2array(GeneralUtility::getUrl($file));
135  }
136  }
137  $modifiedDataStructure['sheets'][$sheetName] = $sheetStructure;
138  }
139  }
140  $result['processedTca']['columns'][$fieldName]['config']['ds'] = $modifiedDataStructure;
141  return $result;
142  }
143 }