‪TYPO3CMS  11.5
SiteTcaInline.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 
24 use TYPO3\CMS\Backend\Utility\BackendUtility;
29 
36 {
43  public function ‪addData(array $result): array
44  {
45  $result = $this->‪addInlineFirstPid($result);
46  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
47  if (!$this->‪isInlineField($fieldConfig)) {
48  continue;
49  }
50  $childTableName = $fieldConfig['config']['foreign_table'] ?? '';
51  if (!in_array($childTableName, ['site_errorhandling', 'site_route', 'site_base_variant'], true)) {
52  throw new \RuntimeException('Inline relation to other tables not implemented', 1522494737);
53  }
54  $result['processedTca']['columns'][$fieldName]['children'] = [];
55  $result = $this->‪resolveSiteRelatedChildren($result, $fieldName);
56  if (!empty($result['processedTca']['columns'][$fieldName]['config']['selectorOrUniqueConfiguration'])) {
57  throw new \RuntimeException('selectorOrUniqueConfiguration not implemented in sites module', 1624313533);
58  }
59  }
60 
61  return $result;
62  }
63 
70  protected function ‪isInlineField(array $fieldConfig): bool
71  {
72  return !empty($fieldConfig['config']['type']) && $fieldConfig['config']['type'] === 'inline';
73  }
74 
83  protected function ‪addInlineFirstPid(array $result): array
84  {
85  if ($result['inlineFirstPid'] === null) {
86  $table = $result['tableName'];
87  $row = $result['databaseRow'];
88  // If the parent is a page, use the uid(!) of the (new?) page as pid for the child records:
89  if ($table === 'pages') {
90  $liveVersionId = BackendUtility::getLiveVersionIdOfRecord('pages', $row['uid']);
91  $pid = $liveVersionId ?? $row['uid'];
92  } elseif (($row['pid'] ?? 0) < 0) {
93  $prevRec = BackendUtility::getRecord($table, (int)abs($row['pid']));
94  $pid = $prevRec['pid'];
95  } else {
96  $pid = $row['pid'] ?? 0;
97  }
99  $pageRecord = BackendUtility::getRecord('pages', (int)$pid);
100  if (($pageRecord[‪$GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'] ?? null] ?? 0) > 0) {
101  $pid = (int)$pageRecord[‪$GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField']];
102  }
103  } elseif (strpos($pid, 'NEW') !== 0) {
104  throw new \RuntimeException(
105  'inlineFirstPid should either be an integer or a "NEW..." string',
106  1521220141
107  );
108  }
109  $result['inlineFirstPid'] = $pid;
110  }
111  return $result;
112  }
113 
122  protected function ‪resolveSiteRelatedChildren(array $result, string $fieldName): array
123  {
124  $connectedUids = [];
125  if ($result['command'] === 'edit') {
126  $siteConfigurationForPageUid = (int)$result['databaseRow']['rootPageId'][0];
127  $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
128  try {
129  $site = $siteFinder->getSiteByRootPageId($siteConfigurationForPageUid);
130  } catch (‪SiteNotFoundException $e) {
131  $site = null;
132  }
133  $siteConfiguration = $site ? $site->getConfiguration() : [];
134  if (is_array($siteConfiguration[$fieldName] ?? false)) {
135  $connectedUids = array_keys($siteConfiguration[$fieldName]);
136  }
137  }
138 
139  $result['databaseRow'][$fieldName] = implode(',', $connectedUids);
140  if ($result['inlineCompileExistingChildren']) {
141  foreach ($connectedUids as $uid) {
142  if (strpos((string)$uid, 'NEW') !== 0) {
143  $compiledChild = $this->‪compileChild($result, $fieldName, (int)$uid);
144  $result['processedTca']['columns'][$fieldName]['children'][] = $compiledChild;
145  }
146  }
147  }
148 
149  return $result;
150  }
151 
160  protected function ‪compileChild(array $result, string $parentFieldName, int $childUid): array
161  {
162  $parentConfig = $result['processedTca']['columns'][$parentFieldName]['config'];
163  $childTableName = $parentConfig['foreign_table'];
164 
165  $inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
166  $inlineStackProcessor->initializeByGivenStructure($result['inlineStructure']);
167  $inlineTopMostParent = $inlineStackProcessor->getStructureLevel(0);
168 
169  $formDataGroup = GeneralUtility::makeInstance(SiteConfigurationDataGroup::class);
170  $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
171  $formDataCompilerInput = [
172  'command' => 'edit',
173  'tableName' => $childTableName,
174  'vanillaUid' => $childUid,
175  // Give incoming returnUrl down to children so they generate a returnUrl back to
176  // the originally opening record, also see "originalReturnUrl" in inline container
177  // and FormInlineAjaxController
178  'returnUrl' => $result['returnUrl'],
179  'isInlineChild' => true,
180  'inlineStructure' => $result['inlineStructure'],
181  'inlineExpandCollapseStateArray' => $result['inlineExpandCollapseStateArray'],
182  'inlineFirstPid' => $result['inlineFirstPid'],
183  'inlineParentConfig' => $parentConfig,
184 
185  // values of the current parent element
186  // it is always a string either an id or new...
187  'inlineParentUid' => $result['databaseRow']['uid'],
188  'inlineParentTableName' => $result['tableName'],
189  'inlineParentFieldName' => $parentFieldName,
190 
191  // values of the top most parent element set on first level and not overridden on following levels
192  'inlineTopMostParentUid' => $result['inlineTopMostParentUid'] ?: ($inlineTopMostParent['uid'] ?? null),
193  'inlineTopMostParentTableName' => $result['inlineTopMostParentTableName'] ?: ($inlineTopMostParent['table'] ?? ''),
194  'inlineTopMostParentFieldName' => $result['inlineTopMostParentFieldName'] ?: ($inlineTopMostParent['field'] ?? ''),
195  ];
196 
197  if (($parentConfig['foreign_selector'] ?? false) && ($parentConfig['appearance']['useCombination'] ?? false)) {
198  throw new \RuntimeException('useCombination not implemented in sites module', 1522493097);
199  }
200  return $formDataCompiler->compile($formDataCompilerInput);
201  }
202 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\SiteTcaInline\addInlineFirstPid
‪array addInlineFirstPid(array $result)
Definition: SiteTcaInline.php:83
‪TYPO3\CMS\Backend\Form\FormDataProvider\SiteTcaInline\addData
‪array addData(array $result)
Definition: SiteTcaInline.php:43
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractDatabaseRecordProvider
Definition: AbstractDatabaseRecordProvider.php:30
‪TYPO3\CMS\Backend\Form\FormDataProvider\SiteTcaInline\isInlineField
‪bool isInlineField(array $fieldConfig)
Definition: SiteTcaInline.php:70
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Backend\Form\FormDataProvider\SiteTcaInline\compileChild
‪array compileChild(array $result, string $parentFieldName, int $childUid)
Definition: SiteTcaInline.php:160
‪TYPO3\CMS\Backend\Form\FormDataProvider\SiteTcaInline
Definition: SiteTcaInline.php:36
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Backend\Form\FormDataProvider\SiteTcaInline\resolveSiteRelatedChildren
‪array resolveSiteRelatedChildren(array $result, string $fieldName)
Definition: SiteTcaInline.php:122
‪TYPO3\CMS\Backend\Form\InlineStackProcessor
Definition: InlineStackProcessor.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Form\FormDataCompiler
Definition: FormDataCompiler.php:25
‪TYPO3\CMS\Backend\Form\FormDataGroup\SiteConfigurationDataGroup
Definition: SiteConfigurationDataGroup.php:33