‪TYPO3CMS  ‪main
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 {
36  public static function ‪getDataProvider(array $tcaConfiguration, $table, $field, $currentValue)
37  {
39  $dataProvider = null;
40  if (!isset($tcaConfiguration['treeConfig']) || !is_array($tcaConfiguration['treeConfig'])) {
41  throw new \InvalidArgumentException('TCA Tree configuration is invalid: "treeConfig" array is missing', 1288215890);
42  }
43 
44  if (!empty($tcaConfiguration['treeConfig']['dataProvider'])) {
45  // This is a hack since TYPO3 v10 we use this to inject the EventDispatcher in the first argument
46  // For TYPO3 Core, but this is only possible if the dataProvider is extending from the DatabaseTreeDataProvider
47  // but did NOT use a custom constructor. This way, the original constructor receives the EventDispatcher properly
48  // as first argument. It is encouraged to use a custom constructor that also receives the EventDispatcher
49  // separately.
50  $reflectionClass = new \ReflectionClass($tcaConfiguration['treeConfig']['dataProvider']);
51  if ($reflectionClass->getConstructor()->getDeclaringClass()->getName() === DatabaseTreeDataProvider::class) {
52  $dataProvider = GeneralUtility::makeInstance(
53  $tcaConfiguration['treeConfig']['dataProvider'],
54  GeneralUtility::makeInstance(EventDispatcherInterface::class)
55  );
56  } else {
57  $dataProvider = GeneralUtility::makeInstance(
58  $tcaConfiguration['treeConfig']['dataProvider'],
59  $tcaConfiguration,
60  $table,
61  $field,
62  $currentValue,
63  GeneralUtility::makeInstance(EventDispatcherInterface::class)
64  );
65  }
66  }
67  if (($tcaConfiguration['type'] ?? '') !== 'folder') {
68  if ($dataProvider === null) {
69  $dataProvider = GeneralUtility::makeInstance(DatabaseTreeDataProvider::class);
70  }
71  if (isset($tcaConfiguration['foreign_table'])) {
72  $tableName = $tcaConfiguration['foreign_table'];
73  $dataProvider->setTableName($tableName);
74  if ($tableName == $table) {
75  // The uid of the currently opened row can not be selected in a table relation to "self"
76  $unselectableUids = [$currentValue['uid']];
77  $dataProvider->setItemUnselectableList($unselectableUids);
78  }
79  } else {
80  throw new \InvalidArgumentException('TCA Tree configuration is invalid: "foreign_table" not set', 1288215888);
81  }
82  if (isset($tcaConfiguration['foreign_label'])) {
83  $dataProvider->setLabelField($tcaConfiguration['foreign_label']);
84  } else {
85  $dataProvider->setLabelField(‪$GLOBALS['TCA'][$tableName]['ctrl']['label'] ?? '');
86  }
87  $dataProvider->setTreeId(md5($table . '|' . $field));
88 
89  $treeConfiguration = $tcaConfiguration['treeConfig'];
90  if (isset($treeConfiguration['startingPoints'])) {
91  $dataProvider->setStartingPoints(array_unique(‪GeneralUtility::intExplode(',', (string)$treeConfiguration['startingPoints'])));
92  }
93  if (isset($treeConfiguration['appearance']['expandAll'])) {
94  $dataProvider->setExpandAll((bool)$treeConfiguration['appearance']['expandAll']);
95  }
96  if (isset($treeConfiguration['appearance']['maxLevels'])) {
97  $dataProvider->setLevelMaximum((int)$treeConfiguration['appearance']['maxLevels']);
98  }
99  if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
100  $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
101  } elseif (isset($treeConfiguration['startingPoints'])) {
102  // If there are more than 1 starting points, disable the first level. See description in DatabaseTreeProvider::loadTreeData()
103  $dataProvider->setNonSelectableLevelList(substr_count($treeConfiguration['startingPoints'], ',') > 0 ? '0' : '');
104  }
105  if (isset($treeConfiguration['childrenField'])) {
106  $dataProvider->setLookupMode(‪DatabaseTreeDataProvider::MODE_CHILDREN);
107  $dataProvider->setLookupField($treeConfiguration['childrenField']);
108  } elseif (isset($treeConfiguration['parentField'])) {
109  $dataProvider->setLookupMode(‪DatabaseTreeDataProvider::MODE_PARENT);
110  $dataProvider->setLookupField($treeConfiguration['parentField']);
111  } else {
112  throw new \InvalidArgumentException('TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set', 1288215889);
113  }
114  } elseif ($dataProvider === null) {
115  throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "type=folder" not implemented yet', 1288215892);
116  }
117  return $dataProvider;
118  }
119 }
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
Definition: DatabaseTreeDataProvider.php:41
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider\MODE_PARENT
‪const MODE_PARENT
Definition: DatabaseTreeDataProvider.php:43
‪TYPO3\CMS\Core\Tree\TableConfiguration\TreeDataProviderFactory\getDataProvider
‪static DatabaseTreeDataProvider getDataProvider(array $tcaConfiguration, $table, $field, $currentValue)
Definition: TreeDataProviderFactory.php:36
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider\MODE_CHILDREN
‪const MODE_CHILDREN
Definition: DatabaseTreeDataProvider.php:42
‪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\Tree\TableConfiguration\TreeDataProviderFactory
Definition: TreeDataProviderFactory.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static list< int > intExplode(string $delimiter, string $string, bool $removeEmptyValues=false)
Definition: GeneralUtility.php:756