[BUGFIX] Reimplement pageTS support to selectTree 99/47799/2
authorStefan Busemann <stefan.busemann@in2code.de>
Fri, 4 Mar 2016 20:10:30 +0000 (21:10 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 19 Apr 2016 19:46:50 +0000 (21:46 +0200)
With TCA type=select and renderType=selectTree certain TCA
config options can be modified via pageTS. Those that influence
the data fetching are now properly handled within the data
provider that gathers the tree items.

Example:
    TCEFORM.pages.categories.config.treeConfig.rootUid = 2

Release: master, 7.6
Resolves: #72961
Change-Id: I6a452c1439b0fd1f59c23b764b35cc8e34de7aef
Reviewed-on: https://review.typo3.org/47799
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Form/Element/SelectTreeElement.php
typo3/sysext/backend/Classes/Form/FormDataProvider/TcaSelectTreeItems.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaSelectTreeItemsTest.php

index 2f3ff39..3899a09 100644 (file)
@@ -22,7 +22,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
 /**
  * Render data as a tree.
  *
- * Typically rendered for config [type=select, renderType=selectTree
+ * Typically rendered for config type=select, renderType=selectTree
  */
 class SelectTreeElement extends AbstractFormElement
 {
index 0af0f80..dad2eea 100644 (file)
@@ -81,6 +81,26 @@ class TcaSelectTreeItems extends AbstractItemProvider implements FormDataProvide
             // Keys may contain table names, so a numeric array is created
             $fieldConfig['config']['items'] = array_values($fieldConfig['config']['items']);
 
+            // A couple of tree specific config parameters can be overwritten via page TS.
+            // Pick those that influence the data fetching and write them into the config
+            // given to the tree data provider
+            if (isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['config.']['treeConfig.'])) {
+                $pageTsConfig = $result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['config.']['treeConfig.'];
+                // If rootUid is set in pageTsConfig, use it
+                if (isset($pageTsConfig['rootUid'])) {
+                    $fieldConfig['config']['treeConfig']['rootUid'] = (int)$pageTsConfig['rootUid'];
+                }
+                if (isset($pageTsConfig['appearance.']['expandAll'])) {
+                    $fieldConfig['config']['treeConfig']['appearance']['expandAll'] = (bool)$pageTsConfig['appearance.']['expandAll'];
+                }
+                if (isset($pageTsConfig['appearance.']['maxLevels'])) {
+                    $fieldConfig['config']['treeConfig']['appearance']['maxLevels'] = (int)$pageTsConfig['appearance.']['maxLevels'];
+                }
+                if (isset($pageTsConfig['appearance.']['nonSelectableLevels'])) {
+                    $fieldConfig['config']['treeConfig']['appearance']['nonSelectableLevels'] = $pageTsConfig['appearance.']['nonSelectableLevels'];
+                }
+            }
+
             $fieldConfig['config']['treeData'] = $this->renderTree($result, $fieldConfig, $fieldName, $staticItems);
 
             $result['processedTca']['columns'][$fieldName] = $fieldConfig;
index 577cf64..2f97747 100644 (file)
@@ -116,4 +116,76 @@ class TcaSelectTreeItemsTest extends UnitTestCase
         ];
         $this->assertEquals($expected, $this->subject->addData($input));
     }
+
+    /**
+     * @test
+     */
+    public function addDataHandsPageTsConfigSettingsOverToTableConfigurationTree()
+    {
+        $GLOBALS['TCA']['foreignTable'] = [];
+
+        /** @var DatabaseConnection|ObjectProphecy $database */
+        $database = $this->prophesize(DatabaseConnection::class);
+        $GLOBALS['TYPO3_DB'] = $database->reveal();
+
+        /** @var BackendUserAuthentication|ObjectProphecy $backendUserProphecy */
+        $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
+        $GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
+
+        /** @var DatabaseTreeDataProvider|ObjectProphecy $treeDataProviderProphecy */
+        $treeDataProviderProphecy = $this->prophesize(DatabaseTreeDataProvider::class);
+        GeneralUtility::addInstance(DatabaseTreeDataProvider::class, $treeDataProviderProphecy->reveal());
+
+        /** @var  TableConfigurationTree|ObjectProphecy $treeDataProviderProphecy */
+        $tableConfigurationTreeProphecy = $this->prophesize(TableConfigurationTree::class);
+        GeneralUtility::addInstance(TableConfigurationTree::class, $tableConfigurationTreeProphecy->reveal());
+
+        $input = [
+            'tableName' => 'aTable',
+            'databaseRow' => [
+                'aField' => '1'
+            ],
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [
+                        'config' => [
+                            'type' => 'select',
+                            'renderType' => 'selectTree',
+                            'treeConfig' => [
+                                'childrenField' => 'childrenField'
+                            ],
+                            'foreign_table' => 'foreignTable',
+                            'items' => [],
+                            'maxitems' => 1
+                        ],
+                    ],
+                ],
+            ],
+            'pageTsConfig' => [
+                'TCEFORM.' => [
+                    'aTable.' => [
+                        'aField.' => [
+                            'config.' => [
+                                'treeConfig.' => [
+                                    'rootUid' => '42',
+                                    'appearance.' => [
+                                        'expandAll' => 1,
+                                        'maxLevels' => 4,
+                                        'nonSelectableLevels' => '0,1',
+                                    ],
+                                ],
+                            ],
+                        ],
+                    ],
+                ],
+            ],
+        ];
+
+        $this->subject->addData($input);
+
+        $treeDataProviderProphecy->setRootUid(42)->shouldHaveBeenCalled();
+        $treeDataProviderProphecy->setExpandAll(true)->shouldHaveBeenCalled();
+        $treeDataProviderProphecy->setLevelMaximum(4)->shouldHaveBeenCalled();
+        $treeDataProviderProphecy->setNonSelectableLevelList('0,1')->shouldHaveBeenCalled();
+    }
 }