[TASK] Add possibility for a custom TCA TreeProvider
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Tree / TableConfiguration / TreeDataProviderFactory.php
index 2242ed3..ff8079a 100644 (file)
@@ -39,18 +39,30 @@ class TreeDataProviderFactory {
         * Gets the data provider, depending on TCA configuration
         *
         * @param array $tcaConfiguration
-        * @return \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
+        * @param $table
+        * @param $field
+        * @param $currentValue
+        * @return DatabaseTreeDataProvider
         * @throws \InvalidArgumentException
         */
        static public function getDataProvider(array $tcaConfiguration, $table, $field, $currentValue) {
+               /** @var $dataProvider \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider */
                $dataProvider = NULL;
+               if (!isset($tcaConfiguration['treeConfig']) | !is_array($tcaConfiguration['treeConfig'])) {
+                       throw new \InvalidArgumentException('TCA Tree configuration is invalid: "treeConfig" array is missing', 1288215890);
+               }
+
+               if (!empty($tcaConfiguration['treeConfig']['dataProvider'])) {
+                       $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($tcaConfiguration['treeConfig']['dataProvider'], $tcaConfiguration, $table, $field, $currentValue);
+               }
                if (!isset($tcaConfiguration['internal_type'])) {
                        $tcaConfiguration['internal_type'] = 'db';
                }
-               if ($tcaConfiguration['internal_type'] == 'db') {
+               if ($tcaConfiguration['internal_type'] === 'db') {
                        $unselectableUids = array();
-                       /** @var $dataProvider \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider */
-                       $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Tree\\TableConfiguration\\DatabaseTreeDataProvider');
+                       if ($dataProvider === NULL) {
+                               $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Tree\\TableConfiguration\\DatabaseTreeDataProvider');
+                       }
                        if (isset($tcaConfiguration['foreign_table'])) {
                                $tableName = $tcaConfiguration['foreign_table'];
                                $dataProvider->setTableName($tableName);
@@ -68,39 +80,37 @@ class TreeDataProviderFactory {
                        }
                        $dataProvider->setTreeId(md5($table . '|' . $field));
                        $dataProvider->setSelectedList($currentValue);
-                       if (isset($tcaConfiguration['treeConfig']) && is_array($tcaConfiguration['treeConfig'])) {
-                               $treeConfiguration = $tcaConfiguration['treeConfig'];
-                               if (isset($treeConfiguration['rootUid'])) {
-                                       $dataProvider->setRootUid(intval($treeConfiguration['rootUid']));
-                               }
-                               if (isset($treeConfiguration['appearance']['expandAll'])) {
-                                       $dataProvider->setExpandAll((bool) $treeConfiguration['appearance']['expandAll']);
-                               }
-                               if (isset($treeConfiguration['appearance']['maxLevels'])) {
-                                       $dataProvider->setLevelMaximum(intval($treeConfiguration['appearance']['maxLevels']));
-                               }
-                               if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
-                                       $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
-                               } elseif (isset($treeConfiguration['rootUid'])) {
-                                       $dataProvider->setNonSelectableLevelList('');
-                               }
-                               if (isset($treeConfiguration['childrenField'])) {
-                                       $dataProvider->setLookupMode(\t3lib_tree_tca_DatabaseTreeDataProvider::MODE_CHILDREN);
-                                       $dataProvider->setLookupField($treeConfiguration['childrenField']);
-                               } elseif (isset($treeConfiguration['parentField'])) {
-                                       $dataProvider->setLookupMode(\t3lib_tree_tca_DatabaseTreeDataProvider::MODE_PARENT);
-                                       $dataProvider->setLookupField($treeConfiguration['parentField']);
-                               } else {
-                                       throw new \InvalidArgumentException('TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set', 1288215889);
-                               }
-                               $dataProvider->setItemUnselectableList($unselectableUids);
+
+                       $treeConfiguration = $tcaConfiguration['treeConfig'];
+                       if (isset($treeConfiguration['rootUid'])) {
+                               $dataProvider->setRootUid(intval($treeConfiguration['rootUid']));
+                       }
+                       if (isset($treeConfiguration['appearance']['expandAll'])) {
+                               $dataProvider->setExpandAll((bool) $treeConfiguration['appearance']['expandAll']);
+                       }
+                       if (isset($treeConfiguration['appearance']['maxLevels'])) {
+                               $dataProvider->setLevelMaximum(intval($treeConfiguration['appearance']['maxLevels']));
+                       }
+                       if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
+                               $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
+                       } elseif (isset($treeConfiguration['rootUid'])) {
+                               $dataProvider->setNonSelectableLevelList('');
+                       }
+                       if (isset($treeConfiguration['childrenField'])) {
+                               $dataProvider->setLookupMode(\t3lib_tree_tca_DatabaseTreeDataProvider::MODE_CHILDREN);
+                               $dataProvider->setLookupField($treeConfiguration['childrenField']);
+                       } elseif (isset($treeConfiguration['parentField'])) {
+                               $dataProvider->setLookupMode(\t3lib_tree_tca_DatabaseTreeDataProvider::MODE_PARENT);
+                               $dataProvider->setLookupField($treeConfiguration['parentField']);
                        } else {
-                               throw new \InvalidArgumentException('TCA Tree configuration is invalid: "treeConfig" array is missing', 1288215890);
+                               throw new \InvalidArgumentException('TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set', 1288215889);
                        }
-               } elseif ($tcaConfiguration['internal_type'] == 'file') {
+                       $dataProvider->setItemUnselectableList($unselectableUids);
+
+               } elseif ($tcaConfiguration['internal_type'] === 'file' && $dataProvider === NULL) {
                        // TODO Not implemented yet
                        throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=file" not implemented yet', 1288215891);
-               } else {
+               } elseif ($dataProvider === NULL) {
                        throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=' . $tcaConfiguration['internal_type'] . '" not implemented yet', 1288215892);
                }
                return $dataProvider;