[BUGFIX] Preserve order of select items in FormEngine 56/62856/4
authorMarkus Klein <markus.klein@typo3.org>
Sun, 12 Jan 2020 18:02:51 +0000 (19:02 +0100)
committerDaniel Goerz <daniel.goerz@posteo.de>
Mon, 13 Jan 2020 11:06:58 +0000 (12:06 +0100)
If a select field is comprised of static and dynamic
(database relation) values the order of items stored in
database is preserved when showing the select field.

Resolves: #87672
Releases: master, 9.5, 8.7
Change-Id: I95ed9d523f68eb106c22bbbcd14e60a17a2c0293
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/62856
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Felix P. <f.pachowsky@neusta.de>
Tested-by: Henning Liebe <h.liebe@neusta.de>
Tested-by: Sascha Rademacher <sascha.rademacher+typo3@gmail.com>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Susanne Moog <look@susi.dev>
Reviewed-by: Felix P. <f.pachowsky@neusta.de>
Reviewed-by: Henning Liebe <h.liebe@neusta.de>
Reviewed-by: Sascha Rademacher <sascha.rademacher+typo3@gmail.com>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaSelectItemsTest.php

index 48871b2..f7d0a6b 100644 (file)
@@ -1268,6 +1268,7 @@ abstract class AbstractItemProvider
                     $result['tableName'],
                     $fieldConfig['config']
                 );
+                $newDatabaseValueArray = array_merge($newDatabaseValueArray, $relationHandler->getValueArray());
             } else {
                 // Non MM relation
                 // If not dealing with MM relations, use default live uid, not versioned uid for record relations
@@ -1279,8 +1280,10 @@ abstract class AbstractItemProvider
                     $result['tableName'],
                     $fieldConfig['config']
                 );
+                $databaseIds = array_merge($newDatabaseValueArray, $relationHandler->getValueArray());
+                // remove all items from the current DB values if not available as relation or static value anymore
+                $newDatabaseValueArray = array_values(array_intersect($currentDatabaseValueArray, $databaseIds));
             }
-            $newDatabaseValueArray = array_merge($newDatabaseValueArray, $relationHandler->getValueArray());
         }
 
         if ($fieldConfig['config']['multiple'] ?? false) {
index dde6c33..a7c9ad9 100644 (file)
@@ -3012,7 +3012,7 @@ class TcaSelectItemsTest extends UnitTestCase
         ];
 
         $expected = $input;
-        $expected['databaseRow']['aField'] = ['foo', 1];
+        $expected['databaseRow']['aField'] = [1, 'foo'];
 
         self::assertEquals($expected, (new TcaSelectItems)->addData($input));
     }