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