[BUGFIX] Properly append/prepend with "addRecord" wizard 26/55826/5
authorMathias Brodala <mbrodala@pagemachine.de>
Tue, 20 Feb 2018 14:47:59 +0000 (15:47 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 1 Mar 2018 21:43:01 +0000 (22:43 +0100)
For relations the internal field value contains a list of UIDs instead
of full related records.

Resolves: #82461
Releases: master, 8.7
Change-Id: Iec2a74fbc8d17636dd8751911984c337d4e99b63
Reviewed-on: https://review.typo3.org/55826
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Kevin Ditscheid <kevinditscheid@gmail.com>
Tested-by: Kevin Ditscheid <kevinditscheid@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Controller/Wizard/AddController.php

index 9409830..f069a3e 100644 (file)
@@ -232,32 +232,38 @@ class AddController extends AbstractWizardController
                             $insertValue
                         );
                     } else {
-                        // Check the row for its datatype. If it is an array it stores the relation
-                        // to other rows. Implode it into a comma separated list to be able to restore the stored
-                        // values after the wizard falls back to the parent record
                         $currentValue = $currentParentRow[$this->P['field']];
-                        if (is_array($currentValue)) {
-                            $currentValue = implode(',', array_column($currentValue, 'uid'));
+
+                        // Normalize CSV values
+                        if (!is_array($currentValue)) {
+                            $currentValue = GeneralUtility::trimExplode(',', $currentValue, true);
                         }
+
+                        // Normalize all items to "<table>_<uid>" format
+                        $currentValue = array_map(function ($item) {
+                            // Handle per-item table for "group" elements
+                            if (is_array($item)) {
+                                $item = $item['table'] . '_' . $item['uid'];
+                            } else {
+                                $item = $this->table . '_' . $item;
+                            }
+
+                            return $item;
+                        }, $currentValue);
+
                         switch ((string)$this->P['params']['setValue']) {
                             case 'set':
-                                $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $recordId;
+                                $currentValue = [$recordId];
                                 break;
                             case 'append':
-                                $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $currentValue . ',' . $recordId;
+                                $currentValue[] = $recordId;
                                 break;
                             case 'prepend':
-                                $data[$this->P['table']][$this->P['uid']][$this->P['field']] = $recordId . ',' . $currentValue;
+                                array_unshift($currentValue, $recordId);
                                 break;
                         }
-                        $data[$this->P['table']][$this->P['uid']][$this->P['field']] = implode(
-                            ',',
-                            GeneralUtility::trimExplode(
-                                ',',
-                                $data[$this->P['table']][$this->P['uid']][$this->P['field']],
-                                true
-                            )
-                        );
+
+                        $data[$this->P['table']][$this->P['uid']][$this->P['field']] = implode(',', $currentValue);
                     }
                     // Submit the data:
                     $dataHandler->start($data, []);