‪TYPO3CMS  11.5
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  $tcaConfiguration['internal_type'] = $tcaConfiguration['internal_type'] ?? 'db';
69  if ($tcaConfiguration['internal_type'] === 'db') {
70  if ($dataProvider === null) {
71  $dataProvider = GeneralUtility::makeInstance(DatabaseTreeDataProvider::class);
72  }
73  if (isset($tcaConfiguration['foreign_table'])) {
74  $tableName = $tcaConfiguration['foreign_table'];
75  $dataProvider->setTableName($tableName);
76  if ($tableName == $table) {
77  // The uid of the currently opened row can not be selected in a table relation to "self"
78  $unselectableUids = [$currentValue['uid']];
79  $dataProvider->setItemUnselectableList($unselectableUids);
80  }
81  } else {
82  throw new \InvalidArgumentException('TCA Tree configuration is invalid: "foreign_table" not set', 1288215888);
83  }
84  if (isset($tcaConfiguration['foreign_label'])) {
85  $dataProvider->setLabelField($tcaConfiguration['foreign_label']);
86  } else {
87  $dataProvider->setLabelField(‪$GLOBALS['TCA'][$tableName]['ctrl']['label'] ?? '');
88  }
89  $dataProvider->setTreeId(md5($table . '|' . $field));
90 
91  $treeConfiguration = $tcaConfiguration['treeConfig'];
92  if (isset($treeConfiguration['rootUid'])) {
93  // @deprecated will be removed in v12
94  $dataProvider->setRootUid((int)$treeConfiguration['rootUid']);
95  }
96  if (isset($treeConfiguration['startingPoints'])) {
97  $dataProvider->setStartingPoints(array_unique(‪GeneralUtility::intExplode(',', $treeConfiguration['startingPoints'])));
98  }
99  if (isset($treeConfiguration['appearance']['expandAll'])) {
100  $dataProvider->setExpandAll((bool)$treeConfiguration['appearance']['expandAll']);
101  }
102  if (isset($treeConfiguration['appearance']['maxLevels'])) {
103  $dataProvider->setLevelMaximum((int)$treeConfiguration['appearance']['maxLevels']);
104  }
105  if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
106  $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
107  } elseif (isset($treeConfiguration['startingPoints'])) {
108  // If there are more than 1 starting points, disable the first level. See description in DatabaseTreeProvider::loadTreeData()
109  $dataProvider->setNonSelectableLevelList(substr_count($treeConfiguration['startingPoints'], ',') > 0 ? '0' : '');
110  }
111  if (isset($treeConfiguration['childrenField'])) {
112  $dataProvider->setLookupMode(‪DatabaseTreeDataProvider::MODE_CHILDREN);
113  $dataProvider->setLookupField($treeConfiguration['childrenField']);
114  } elseif (isset($treeConfiguration['parentField'])) {
115  $dataProvider->setLookupMode(‪DatabaseTreeDataProvider::MODE_PARENT);
116  $dataProvider->setLookupField($treeConfiguration['parentField']);
117  } else {
118  throw new \InvalidArgumentException('TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set', 1288215889);
119  }
120  } elseif ($dataProvider === null) {
121  throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=' . $tcaConfiguration['internal_type'] . '" not implemented yet', 1288215892);
122  }
123  return $dataProvider;
124  }
125 }
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
Definition: DatabaseTreeDataProvider.php:37
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider\MODE_PARENT
‪const MODE_PARENT
Definition: DatabaseTreeDataProvider.php:39
‪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:38
‪TYPO3\CMS\Core\Tree\TableConfiguration
Definition: AbstractTableConfigurationTreeDataProvider.php:16
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static int[] intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:927
‪TYPO3\CMS\Core\Tree\TableConfiguration\TreeDataProviderFactory
Definition: TreeDataProviderFactory.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50