[TASK] Free FormEngine from _GP usage 90/56390/3
authorChristian Kuhn <lolli@schwarzbu.ch>
Wed, 21 Mar 2018 09:42:22 +0000 (10:42 +0100)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Wed, 21 Mar 2018 19:09:07 +0000 (20:09 +0100)
Solves a todo in FormEngine: A data provider still
fetched data from _GP instead of relying of this data
being set by a controller: "defaultValues" for new
records can now be set by controllers as input $result
array key.

Resolves: #84506
Releases: master
Change-Id: I174eea2d5954b7ac29edef1007c1446c7816b0ed
Reviewed-on: https://review.typo3.org/56390
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
typo3/sysext/backend/Classes/Controller/EditDocumentController.php
typo3/sysext/backend/Classes/Form/FormDataCompiler.php
typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php

index 3ba5fd3..f60b2ef 100644 (file)
@@ -548,6 +548,7 @@ class EditDocumentController
         $this->uc = $parsedBody['uc'] ?? $queryParams['uc'] ?? null;
 
         // Set overrideVals as default values if defVals does not exist.
+        // @todo: Why?
         if (!is_array($this->defVals) && is_array($this->overrideVals)) {
             $this->defVals = $this->overrideVals;
         }
@@ -1221,6 +1222,9 @@ class EditDocumentController
                                 if (is_array($this->overrideVals) && is_array($this->overrideVals[$table])) {
                                     $formDataCompilerInput['overrideValues'] = $this->overrideVals[$table];
                                 }
+                                if (!empty($this->defVals) && is_array($this->defVals)) {
+                                    $formDataCompilerInput['defaultValues'] = $this->defVals;
+                                }
 
                                 $formData = $formDataCompiler->compile($formDataCompilerInput);
 
index e31c413..a881e8d 100644 (file)
@@ -291,9 +291,15 @@ class FormDataCompiler
             'tabAndInlineStack' => [],
             'inlineData' => [],
             'inlineStructure' => [],
+
             // This array of fields will be set as hidden-fields instead of rendered normally!
             // This is used by EditDocumentController to force some field values if set as "overrideVals" in _GP
             'overrideValues' => [],
+            // Default values for fields. This is for example used in DatabaseRowInitializeNew data provider to set
+            // fields to specific values if new records are created. Values are often carried around as "defVals" GET
+            // parameters and hand over by controllers to FormEngine using this array. Array structure is
+            // ['aTableName']['aFieldName'] = 'aValue'.
+            'defaultValues' => [],
 
             // This array must NOT be set / manipulated by data providers but is instead used by the render part
             // of FormEngine to add runtime data. Containers and elements add data here which is given to
index 2775fcf..4ce2c47 100644 (file)
@@ -46,7 +46,7 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface
         $result = $this->setDefaultsFromUserTsConfig($result);
         $result = $this->setDefaultsFromPageTsConfig($result);
         $result = $this->setDefaultsFromNeighborRow($result);
-        $result = $this->setDefaultsFromDevVals($result);
+        $result = $this->setDefaultsFromDefaultValues($result);
         $result = $this->setDefaultsFromInlineRelations($result);
         $result = $this->setDefaultsFromInlineParentLanguage($result);
         $result = $this->setPid($result);
@@ -122,23 +122,19 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface
     }
 
     /**
-     * Apply default values from GET / POST
-     *
-     * @todo: Fetch this stuff from request object as soon as modules were moved to PSR-7,
-     * @todo: or hand values over via $result array, so the _GP access is transferred to
-     * @todo: controllers concern.
+     * Apply default values.
+     * These are typically carried around as "defVals" GET vars and set by controllers
+     * in $result['defaultValues'] array as init values.
      *
      * @param array $result Result array
      * @return array Modified result array
      */
-    protected function setDefaultsFromDevVals(array $result)
+    protected function setDefaultsFromDefaultValues(array $result)
     {
         $tableName = $result['tableName'];
-        $defaultValuesFromGetPost = GeneralUtility::_GP('defVals');
-        if (isset($defaultValuesFromGetPost[$tableName])
-            && is_array($defaultValuesFromGetPost[$tableName])
-        ) {
-            foreach ($defaultValuesFromGetPost[$tableName] as $fieldName => $fieldValue) {
+        $defaultValues = $result['defaultValues'] ?? [];
+        if (isset($defaultValues[$tableName]) && is_array($defaultValues[$tableName])) {
+            foreach ($defaultValues[$tableName] as $fieldName => $fieldValue) {
                 if (isset($result['processedTca']['columns'][$fieldName])) {
                     $result['databaseRow'][$fieldName] = $fieldValue;
                 }
index 479756b..2c3f183 100644 (file)
@@ -333,7 +333,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
     /**
      * @test
      */
-    public function addDataSetsDefaultDataFromGetIfColumnIsDefinedInTca()
+    public function addDataSetsDefaultDataFromDefaultValuesIfColumnIsDefinedInTca()
     {
         $input = [
             'command' => 'new',
@@ -347,92 +347,15 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
                 'columns' => [
                     'aField' => [],
                 ],
-            ]
-        ];
-        $GLOBALS['_GET'] = [
-            'defVals' => [
-                'aTable' => [
-                    'aField' => 'getValue',
-                ],
             ],
-        ];
-        $expected = [
-            'aField' => 'getValue',
-            'pid' => 23,
-        ];
-        $result = (new DatabaseRowInitializeNew)->addData($input);
-        $this->assertSame($expected, $result['databaseRow']);
-    }
-
-    /**
-     * @test
-     */
-    public function addDataSetsDefaultDataFromPostIfColumnIsDefinedInTca()
-    {
-        $input = [
-            'command' => 'new',
-            'tableName' => 'aTable',
-            'vanillaUid' => 23,
-            'neighborRow' => null,
-            'inlineChildChildUid' => null,
-            'isInlineChild' => false,
-            'databaseRow' => [],
-            'processedTca' => [
-                'columns' => [
-                    'aField' => [],
-                ],
-            ]
-        ];
-        $GLOBALS['_POST'] = [
-            'defVals' => [
-                'aTable' => [
-                    'aField' => 'postValue',
-                ],
-            ],
-        ];
-        $expected = [
-            'aField' => 'postValue',
-            'pid' => 23,
-        ];
-        $result = (new DatabaseRowInitializeNew)->addData($input);
-        $this->assertSame($expected, $result['databaseRow']);
-    }
-
-    /**
-     * @test
-     */
-    public function addDataSetsPrioritizesDefaultPostOverDefaultGet()
-    {
-        $input = [
-            'command' => 'new',
-            'tableName' => 'aTable',
-            'vanillaUid' => 23,
-            'neighborRow' => null,
-            'inlineChildChildUid' => null,
-            'isInlineChild' => false,
-            'databaseRow' => [],
-            'processedTca' => [
-                'columns' => [
-                    'aField' => [],
-                ],
-            ]
-        ];
-        $GLOBALS['_GET'] = [
-            'defVals' => [
+            'defaultValues' => [
                 'aTable' => [
                     'aField' => 'getValue',
                 ],
             ],
         ];
-        $GLOBALS['_POST'] = [
-            'defVals' => [
-                'aTable' => [
-                    'aField' => 'postValue',
-                ],
-            ],
-        ];
         $expected = [
-            'aField' => 'postValue',
+            'aField' => 'getValue',
             'pid' => 23,
         ];
         $result = (new DatabaseRowInitializeNew)->addData($input);
@@ -442,7 +365,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
     /**
      * @test
      */
-    public function addDataDoesNotSetDefaultDataFromGetPostIfColumnIsMissingInTca()
+    public function addDataDoesNotSetDefaultDataFromDefaultValuesIfColumnIsMissingInTca()
     {
         $input = [
             'command' => 'new',
@@ -461,19 +384,10 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
             ],
             'processedTca' => [
                 'columns' => [],
-            ]
-        ];
-        $GLOBALS['_GET'] = [
-            'defVals' => [
-                'aTable' => [
-                    'aField' => 'getValue',
-                ],
             ],
-        ];
-        $GLOBALS['_POST'] = [
-            'defVals' => [
+            'defaultValues' => [
                 'aTable' => [
-                    'aField' => 'postValue',
+                    'aField' => 'getValue',
                 ],
             ],
         ];
@@ -487,7 +401,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
     /**
      * @test
      */
-    public function addDataSetsDefaultDataOverrulingGetPost()
+    public function addDataSetsDefaultDataOverrulesOtherDefauls()
     {
         $input = [
             'command' => 'new',
@@ -521,13 +435,11 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
                     'aField' => [],
                 ],
             ],
-        ];
-        $GLOBALS['_POST'] = [
-            'defVals' => [
+            'defaultValues' => [
                 'aTable' => [
                     'aField' => 'postValue',
                 ],
-            ],
+            ]
         ];
         $expected = [
             'aField' => 'postValue',