‪TYPO3CMS  10.4
TreeDataProviderFactory.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\EventDispatcher\EventDispatcherInterface;
20 
26 {
37  public static function ‪getDataProvider(array $tcaConfiguration, $table, $field, $currentValue)
38  {
40  $dataProvider = null;
41  if (!isset($tcaConfiguration['treeConfig']) || !is_array($tcaConfiguration['treeConfig'])) {
42  throw new \InvalidArgumentException('TCA Tree configuration is invalid: "treeConfig" array is missing', 1288215890);
43  }
44 
45  if (!empty($tcaConfiguration['treeConfig']['dataProvider'])) {
46  // This is a hack since TYPO3 v10 we use this to inject the EventDispatcher in the first argument
47  // For TYPO3 Core, but this is only possible if the dataProvider is extending from the DatabaseTreeDataProvider
48  // but did NOT use a custom constructor. This way, the original constructor receives the EventDispatcher properly
49  // as first argument. It is encouraged to use a custom constructor that also receives the EventDispatcher
50  // separately.
51  $reflectionClass = new \ReflectionClass($tcaConfiguration['treeConfig']['dataProvider']);
52  if ($reflectionClass->getConstructor()->getDeclaringClass()->getName() === DatabaseTreeDataProvider::class) {
53  $dataProvider = GeneralUtility::makeInstance(
54  $tcaConfiguration['treeConfig']['dataProvider'],
55  GeneralUtility::makeInstance(EventDispatcherInterface::class)
56  );
57  } else {
58  $dataProvider = GeneralUtility::makeInstance(
59  $tcaConfiguration['treeConfig']['dataProvider'],
60  $tcaConfiguration,
61  $table,
62  $field,
63  $currentValue,
64  GeneralUtility::makeInstance(EventDispatcherInterface::class)
65  );
66  }
67  }
68  if (!isset($tcaConfiguration['internal_type'])) {
69  $tcaConfiguration['internal_type'] = 'db';
70  }
71  if ($tcaConfiguration['internal_type'] === 'db') {
72  if ($dataProvider === null) {
73  $dataProvider = GeneralUtility::makeInstance(DatabaseTreeDataProvider::class);
74  }
75  if (isset($tcaConfiguration['foreign_table'])) {
76  $tableName = $tcaConfiguration['foreign_table'];
77  $dataProvider->setTableName($tableName);
78  if ($tableName == $table) {
79  // The uid of the currently opened row can not be selected in a table relation to "self"
80  $unselectableUids = [$currentValue['uid']];
81  $dataProvider->setItemUnselectableList($unselectableUids);
82  }
83  } else {
84  throw new \InvalidArgumentException('TCA Tree configuration is invalid: "foreign_table" not set', 1288215888);
85  }
86  if (isset($tcaConfiguration['foreign_label'])) {
87  $dataProvider->setLabelField($tcaConfiguration['foreign_label']);
88  } else {
89  $dataProvider->setLabelField(‪$GLOBALS['TCA'][$tableName]['ctrl']['label'] ?? '');
90  }
91  $dataProvider->setTreeId(md5($table . '|' . $field));
92 
93  $treeConfiguration = $tcaConfiguration['treeConfig'];
94  if (isset($treeConfiguration['rootUid'])) {
95  $dataProvider->setRootUid((int)$treeConfiguration['rootUid']);
96  }
97  if (isset($treeConfiguration['appearance']['expandAll'])) {
98  $dataProvider->setExpandAll((bool)$treeConfiguration['appearance']['expandAll']);
99  }
100  if (isset($treeConfiguration['appearance']['maxLevels'])) {
101  $dataProvider->setLevelMaximum((int)$treeConfiguration['appearance']['maxLevels']);
102  }
103  if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
104  $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
105  } elseif (isset($treeConfiguration['rootUid'])) {
106  $dataProvider->setNonSelectableLevelList('');
107  }
108  if (isset($treeConfiguration['childrenField'])) {
109  $dataProvider->setLookupMode(‪DatabaseTreeDataProvider::MODE_CHILDREN);
110  $dataProvider->setLookupField($treeConfiguration['childrenField']);
111  } elseif (isset($treeConfiguration['parentField'])) {
112  $dataProvider->setLookupMode(‪DatabaseTreeDataProvider::MODE_PARENT);
113  $dataProvider->setLookupField($treeConfiguration['parentField']);
114  } else {
115  throw new \InvalidArgumentException('TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set', 1288215889);
116  }
117  } elseif ($dataProvider === null) {
118  throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=' . $tcaConfiguration['internal_type'] . '" not implemented yet', 1288215892);
119  }
120  return $dataProvider;
121  }
122 }
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
Definition: DatabaseTreeDataProvider.php:36
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider\MODE_PARENT
‪const MODE_PARENT
Definition: DatabaseTreeDataProvider.php:42
‪TYPO3\CMS\Core\Tree\TableConfiguration\TreeDataProviderFactory\getDataProvider
‪static DatabaseTreeDataProvider getDataProvider(array $tcaConfiguration, $table, $field, $currentValue)
Definition: TreeDataProviderFactory.php:37
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider\MODE_CHILDREN
‪const MODE_CHILDREN
Definition: DatabaseTreeDataProvider.php:41
‪TYPO3\CMS\Core\Tree\TableConfiguration
Definition: AbstractTableConfigurationTreeDataProvider.php:16
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tree\TableConfiguration\TreeDataProviderFactory
Definition: TreeDataProviderFactory.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46