[BUGFIX] FormEngine databaseRow compatibility with old methods 52/43152/2
authorChristian Kuhn <lolli@schwarzbu.ch>
Wed, 9 Sep 2015 13:12:43 +0000 (15:12 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Wed, 9 Sep 2015 13:29:15 +0000 (15:29 +0200)
Introduce a helper method to fake old databaseRow layout at some
places where old methods expect a flat array.

Resolves: #69618
Releases: master
Change-Id: I3657393844f141623187d01744f812cc6fa1adc1
Reviewed-on: http://review.typo3.org/43152
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Tested-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Form/Container/InlineRecordContainer.php
typo3/sysext/backend/Classes/Form/Utility/FormEngineUtility.php
typo3/sysext/backend/Tests/Unit/Form/Utility/FormEngineUtilityTest.php [new file with mode: 0644]

index 9bade8a..3b61544 100644 (file)
@@ -413,7 +413,7 @@ class InlineRecordContainer extends AbstractContainer {
                                $recTitle = BackendUtility::getNoRecordTitle(TRUE);
                        }
                } else {
-                       $recTitle = BackendUtility::getRecordTitle($foreign_table, $rec, TRUE);
+                       $recTitle = BackendUtility::getRecordTitle($foreign_table, FormEngineUtility::databaseRowCompatibility($rec), TRUE);
                }
 
                $altText = BackendUtility::getRecordIconAltText($rec, $foreign_table);
index eb519f5..f9f6ff0 100644 (file)
@@ -451,6 +451,39 @@ class FormEngineUtility {
        }
 
        /**
+        * Compatibility layer for methods not in FormEngine scope.
+        *
+        * databaseRow was a flat array with single elements in select and group fields as comma separated list.
+        * With new data handling in FormEngine, this is now an array of element values. There are however "old"
+        * methods that still expect the flat array.
+        * This method implodes the array again to fake the old behavior of a database row before it is given
+        * to those methods.
+        *
+        * @param array $row Incoming array
+        * @return array Flat array
+        * @internal
+        */
+       static public function databaseRowCompatibility(array $row) {
+               $newRow = [];
+               foreach ($row as $fieldName => $fieldValue) {
+                       if (!is_array($fieldValue)) {
+                               $newRow[$fieldName] = $fieldValue;
+                       } else {
+                               $newElementValue = [];
+                               foreach ($fieldValue as $itemNumber => $itemValue) {
+                                       if (is_array($itemValue) && array_key_exists(1, $itemValue)) {
+                                               $newElementValue[] = $itemValue[1];
+                                       } else {
+                                               $newElementValue[] = $itemValue;
+                                       }
+                               }
+                               $newRow[$fieldName] = implode(',', $newElementValue);
+                       }
+               }
+               return $newRow;
+       }
+
+       /**
         * @return LanguageService
         */
        static protected function  getLanguageService() {
diff --git a/typo3/sysext/backend/Tests/Unit/Form/Utility/FormEngineUtilityTest.php b/typo3/sysext/backend/Tests/Unit/Form/Utility/FormEngineUtilityTest.php
new file mode 100644 (file)
index 0000000..33f0c9a
--- /dev/null
@@ -0,0 +1,75 @@
+<?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\Utility\FormEngineUtility;
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+
+/**
+ * Test case
+ */
+class FormEngineUtilityTest extends UnitTestCase {
+
+       /**
+        * @test
+        */
+       public function databaseRowCompatibilityKeepsSimpleValue() {
+               $input = [
+                       'uid' => 42,
+                       'title' => 'aTitle',
+               ];
+               $expected = $input;
+               $this->assertEquals($expected, FormEngineUtility::databaseRowCompatibility($input));
+       }
+
+       /**
+        * @test
+        */
+       public function databaseRowCompatibilityImplodesSimpleArray() {
+               $input = [
+                       'uid' => 42,
+                       'simpleArray' => [
+                               0 => 1,
+                               1 => 2,
+                       ],
+               ];
+               $expected = $input;
+               $expected['simpleArray'] = '1,2';
+               $this->assertEquals($expected, FormEngineUtility::databaseRowCompatibility($input));
+       }
+
+       /**
+        * @test
+        */
+       public function databaseRowCompatibilityImplodesSelectArrayWithValuesAtSecondPosition() {
+               $input = [
+                       'uid' => 42,
+                       'simpleArray' => [
+                               0 => [
+                                       0 => 'aLabel',
+                                       1 => 'aValue',
+                               ],
+                               1 => [
+                                       0 => 'anotherLabel',
+                                       1 => 'anotherValue',
+                               ],
+                       ],
+               ];
+               $expected = $input;
+               $expected['simpleArray'] = 'aValue,anotherValue';
+               $this->assertEquals($expected, FormEngineUtility::databaseRowCompatibility($input));
+       }
+
+}