[TASK] FormEngine: Split TcaTypesShowitem provider 29/43329/8
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Tue, 15 Sep 2015 12:35:57 +0000 (14:35 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 19 Sep 2015 10:05:39 +0000 (12:05 +0200)
Split the TcaTypesShowitem provider to only deal with resolving
the showitem list. The actual removal of columns not referenced in
either in the showitem list or in one of the palettes is done by the
TcaTypesRemoveUnusedColumns provider.

Resolves: #69821
Releases: master
Change-Id: I2cf988ca375a09d4f62e2cdbf1566fb9e449447e
Reviewed-on: http://review.typo3.org/43329
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Tested-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
typo3/sysext/backend/Classes/Form/FormDataProvider/TcaTypesRemoveUnusedColumns.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Form/FormDataProvider/TcaTypesShowitem.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaTypesRemoveUnusedColumnsTest.php [new file with mode: 0644]
typo3/sysext/core/Configuration/DefaultConfiguration.php

diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/TcaTypesRemoveUnusedColumns.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/TcaTypesRemoveUnusedColumns.php
new file mode 100644 (file)
index 0000000..51b580b
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+namespace TYPO3\CMS\Backend\Form\FormDataProvider;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Remove fields from columns not in showitem or palette list.
+ * This is a relatively effective performance improvement preventing other
+ * providers from resolving stuff of fields that are not shown later.
+ * Especially effective for fal related tables.
+ */
+class TcaTypesRemoveUnusedColumns implements FormDataProviderInterface {
+
+       /**
+        * Remove unused column fields to speed up further processing.
+        *
+        * @param array $result
+        * @return array
+        */
+       public function addData(array $result) {
+               $recordTypeValue = $result['recordTypeValue'];
+               if (empty($result['processedTca']['types'][$recordTypeValue]['showitem'])
+                       || !is_string($result['processedTca']['types'][$recordTypeValue]['showitem'])
+                       || empty($result['processedTca']['columns'])
+                       || !is_array($result['processedTca']['columns'])
+               ) {
+                       return $result;
+               }
+
+               $showItemFieldString = $result['processedTca']['types'][$recordTypeValue]['showitem'];
+               $showItemFieldArray = GeneralUtility::trimExplode(',', $showItemFieldString, TRUE);
+               $shownColumnFields = [];
+               foreach ($showItemFieldArray as $fieldConfigurationString) {
+                       $fieldConfigurationArray = GeneralUtility::trimExplode(';', $fieldConfigurationString);
+                       $fieldName = $fieldConfigurationArray[0];
+                       if ($fieldName === '--div--') {
+                               continue;
+                       }
+                       if ($fieldName === '--palette--') {
+                               if (isset($fieldConfigurationArray[2])) {
+                                       $paletteName = $fieldConfigurationArray[2];
+                                       if (!empty($result['processedTca']['palettes'][$paletteName]['showitem'])) {
+                                               $paletteFields = GeneralUtility::trimExplode(',', $result['processedTca']['palettes'][$paletteName]['showitem'], TRUE);
+                                               foreach ($paletteFields as $paletteFieldConfiguration) {
+                                                       $paletteFieldConfigurationArray = GeneralUtility::trimExplode(';', $paletteFieldConfiguration);
+                                                       $shownColumnFields[] = $paletteFieldConfigurationArray[0];
+                                               }
+                                       }
+                               }
+                       } else {
+                               $shownColumnFields[] = $fieldName;
+                       }
+               }
+               array_unique($shownColumnFields);
+               $columns = array_keys($result['processedTca']['columns']);
+               foreach ($columns as $column) {
+                       if (!in_array($column, $shownColumnFields)) {
+                               unset($result['processedTca']['columns'][$column]);
+                       }
+               }
+
+               return $result;
+       }
+
+}
index 6fbb4d0..3f6d281 100644 (file)
@@ -19,7 +19,8 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
 
 /**
- * Create final showitem configuration in processedTca for types and palette fields
+ * Create final showitem configuration in processedTca for types and palette
+ * fields
  * Handles all the nasty defails like subtypes_addlist and friends.
  */
 class TcaTypesShowitem implements FormDataProviderInterface {
@@ -69,8 +70,6 @@ class TcaTypesShowitem implements FormDataProviderInterface {
                        }
                }
 
-               $result = $this->removeObsoleteColumns($result);
-
                // Handling of these parameters is finished. Unset them to not allow other handlers to fiddle with it.
                // unset does not throw notice, even if not set
                unset($result['processedTca']['types'][$recordTypeValue]['subtype_value_field']);
@@ -263,54 +262,4 @@ class TcaTypesShowitem implements FormDataProviderInterface {
                return $result;
        }
 
-       /**
-        * Remove fields from columns not in showitem or palette list.
-        * This is a relatively effective performance improvement preventing other providers from
-        * resolving stuff of fields that are not show later. Especially effective for fal related tables
-        *
-        * @param array $result Given result
-        * @return array Modified result
-        * @todo: Unit tests missing for this one
-        */
-       protected function removeObsoleteColumns(array $result) {
-               $recordTypeValue = $result['recordTypeValue'];
-               if (!empty($result['processedTca']['types'][$recordTypeValue]['showitem'])
-                       && is_string($result['processedTca']['types'][$recordTypeValue]['showitem'])
-                       && !empty($result['processedTca']['columns'])
-                       && is_array($result['processedTca']['columns'])
-               ) {
-                       $showItemFieldString = $result['processedTca']['types'][$recordTypeValue]['showitem'];
-                       $showItemFieldArray = GeneralUtility::trimExplode(',', $showItemFieldString, TRUE);
-                       $shownColumnFields = [];
-                       foreach ($showItemFieldArray as $index => $fieldConfigurationString) {
-                               $fieldConfigurationArray = GeneralUtility::trimExplode(';', $fieldConfigurationString);
-                               $fieldName = $fieldConfigurationArray[0];
-                               if ($fieldName === '--div--') {
-                                       continue;
-                               }
-                               if ($fieldName === '--palette--') {
-                                       if (isset($fieldConfigurationArray[2])) {
-                                               $paletteName = $fieldConfigurationArray[2];
-                                               if (!empty($result['processedTca']['palettes'][$paletteName]['showitem'])) {
-                                                       $paletteFields = GeneralUtility::trimExplode(',', $result['processedTca']['palettes'][$paletteName]['showitem'], TRUE);
-                                                       foreach ($paletteFields as $paletteFieldConfiguration) {
-                                                               $paletteFieldConfigurationArray = GeneralUtility::trimExplode(';', $paletteFieldConfiguration);
-                                                               $shownColumnFields[] = $paletteFieldConfigurationArray[0];
-                                                       }
-                                               }
-                                       }
-                               } else {
-                                       $shownColumnFields[] = $fieldName;
-                               }
-                       }
-                       array_unique($shownColumnFields);
-                       $columns = array_keys($result['processedTca']['columns']);
-                       foreach ($columns as $column) {
-                               if (!in_array($column, $shownColumnFields)) {
-                                       unset ($result['processedTca']['columns'][$column]);
-                               }
-                       }
-               }
-               return $result;
-       }
 }
diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaTypesRemoveUnusedColumnsTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaTypesRemoveUnusedColumnsTest.php
new file mode 100644 (file)
index 0000000..cdd8768
--- /dev/null
@@ -0,0 +1,153 @@
+<?php
+namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesRemoveUnusedColumns;
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+
+/**
+ * Test case
+ */
+class TcaTypesRemoveUnusedColumnsTest extends UnitTestCase {
+
+       /**
+        * @var TcaTypesRemoveUnusedColumns
+        */
+       protected $subject;
+
+       public function setUp() {
+               $this->subject = new TcaTypesRemoveUnusedColumns();
+       }
+
+       /**
+        * @test
+        */
+       public function addDataKeepsColumnsFieldReferencedInShowitems() {
+               $input = [
+                       'databaseRow' => [],
+                       'recordTypeValue' => 'aType',
+                       'processedTca' => [
+                               'types' => [
+                                       'aType' => [
+                                               'showitem' => 'keepMe'
+                                       ],
+                               ],
+                               'columns' => [
+                                       'keepMe' => [
+                                               'config' => [
+                                                       'type' => 'input',
+                                               ]
+                                       ],
+                                       'aField' => [
+                                               'config' => [
+                                                       'type' => 'input',
+                                               ]
+                                       ]
+                               ]
+                       ]
+               ];
+
+               $expected = $input;
+               unset($expected['processedTca']['columns']['aField']);
+
+               $this->assertSame($expected, $this->subject->addData($input));
+       }
+
+       /**
+        * @test
+        */
+       public function addDataKeepsColumnsFieldReferencedInPalette() {
+               $input = [
+                       'databaseRow' => [],
+                       'recordTypeValue' => 'aType',
+                       'processedTca' => [
+                               'types' => [
+                                       'aType' => [
+                                               'showitem' => '--palette--;;aPalette'
+                                       ],
+                               ],
+                               'palettes' => [
+                                       'aPalette' => array(
+                                               'showitem' => 'keepMe',
+                                               'canNotCollapse' => TRUE
+                                       ),
+                               ],
+                               'columns' => [
+                                       'keepMe' => [
+                                               'config' => [
+                                                       'type' => 'input',
+                                               ]
+                                       ],
+                                       'bField' => [
+                                               'config' => [
+                                                       'type' => 'input',
+                                               ]
+                                       ]
+                               ]
+                       ]
+               ];
+
+               $expected = $input;
+               unset($expected['processedTca']['columns']['bField']);
+
+               $this->assertSame($expected, $this->subject->addData($input));
+       }
+
+       /**
+        * @test
+        */
+       public function addDataRemovesColumnsNotReferencedInShowitemOrPalette() {
+               $input = [
+                       'databaseRow' => [],
+                       'recordTypeValue' => 'aType',
+                       'processedTca' => [
+                               'types' => [
+                                       'aType' => [
+                                               'showitem' => '--palette--;;aPalette, anotherField'
+                                       ],
+                               ],
+                               'palettes' => [
+                                       'aPalette' => array(
+                                               'showitem' => 'aField',
+                                               'canNotCollapse' => TRUE
+                                       ),
+                               ],
+                               'columns' => [
+                                       'aField' => [
+                                               'config' => [
+                                                       'type' => 'input',
+                                               ]
+                                       ],
+                                       'removeMe' => [
+                                               'config' => [
+                                                       'type' => 'input',
+                                               ]
+                                       ],
+                                       'anotherField' => [
+                                               'config' => [
+                                                       'type' => 'input',
+                                               ]
+                                       ]
+                               ]
+                       ]
+               ];
+
+               $expected = $input;
+               unset($expected['processedTca']['columns']['removeMe']);
+
+               $this->assertSame($expected, $this->subject->addData($input));
+       }
+
+}
index 7218fd0..ec3c1c4 100644 (file)
@@ -409,12 +409,17 @@ return array(
                                                        \TYPO3\CMS\Backend\Form\FormDataProvider\TcaColumnsOverrides::class
                                                ),
                                        ),
+                                       \TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesRemoveUnusedColumns::class => array(
+                                               'depends' => array(
+                                                       \TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem::class
+                                               ),
+                                       ),
                                        \TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexFetch::class => array(
                                                'depends' => array(
                                                        \TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class,
                                                        \TYPO3\CMS\Backend\Form\FormDataProvider\UserTsConfig::class,
                                                        \TYPO3\CMS\Backend\Form\FormDataProvider\PageTsConfigMerged::class,
-                                                       \TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem::class,
+                                                       \TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesRemoveUnusedColumns::class,
                                                ),
                                        ),
                                        \TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare::class => array(
@@ -445,6 +450,7 @@ return array(
                                                        \TYPO3\CMS\Backend\Form\FormDataProvider\PageTsConfigMerged::class,
                                                        \TYPO3\CMS\Backend\Form\FormDataProvider\InitializeProcessedTca::class,
                                                        \TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem::class,
+                                                       \TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesRemoveUnusedColumns::class,
                                                        \TYPO3\CMS\Backend\Form\FormDataProvider\TcaCheckboxItems::class,
                                                        // GeneralUtility::getFlexFormDS() needs unchanged databaseRow values as string
                                                        \TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexFetch::class,