From 7e2928e52ff5ae81fd9c09d9a9b1e77c34c388db Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Chris=20M=C3=BCller?= <typo3@krue.ml>
Date: Thu, 8 Aug 2019 23:21:49 +0200
Subject: [PATCH] [BUGFIX] Set parent uid in newly created IRRE child record
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When creating a new IRRE child element the uid of the parent element
was not set. This is a problem when you use an itemsProcFunc for a
select column which relies on the parent's element uid to show
according values. With the parent uid you can query the stored values.

Resolves: #63777
Releases: master, 9.5
Change-Id: I898de29020bf68e25de9ef1d80cc20353a635524
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61480
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Henning Liebe <h.liebe@neusta.de>
Reviewed-by: Sascha Rademacher <sascha.rademacher+typo3@gmail.com>
Reviewed-by: Felix P. <f.pachowsky@neusta.de>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../DatabaseRowInitializeNew.php              | 19 +++++++++++++++
 .../DatabaseRowInitializeNewTest.php          | 24 +++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php
index 4ce2c47e6a73..80f3f4149893 100644
--- a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php
+++ b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php
@@ -49,6 +49,7 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface
         $result = $this->setDefaultsFromDefaultValues($result);
         $result = $this->setDefaultsFromInlineRelations($result);
         $result = $this->setDefaultsFromInlineParentLanguage($result);
+        $result = $this->setDefaultsFromInlineParentUid($result);
         $result = $this->setPid($result);
 
         return $result;
@@ -223,6 +224,24 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface
         return $result;
     }
 
+    /**
+     * Set the parent uid of inline relations created via ajax to the corresponding foreign field
+     *
+     * @param array $result Result array
+     * @return array
+     */
+    protected function setDefaultsFromInlineParentUid(array $result): array
+    {
+        $isInlineChild = $result['isInlineChild'] ?? false;
+        $parentField = $result['inlineParentConfig']['foreign_field'] ?? false;
+
+        if ($isInlineChild && $parentField && !empty($result['inlineParentUid'])) {
+            $result['databaseRow'][$parentField] = $result['inlineParentUid'];
+        }
+
+        return $result;
+    }
+
     /**
      * Set the pid. This is either the vanillaUid (see description in FormDataCompiler),
      * or a pid given by pageTsConfig for inline children.
diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php
index 0fcb379a1450..3c47a36d37d2 100644
--- a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php
+++ b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php
@@ -709,4 +709,28 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
         $expected['databaseRow']['pid'] = 42;
         self::assertSame($expected, (new DatabaseRowInitializeNew)->addData($input));
     }
+
+    /**
+     * @test
+     */
+    public function addDataSetsUidOfParentFieldIfRecordIsInlineChild()
+    {
+        $input = [
+            'command' => 'new',
+            'tableName' => 'aTable',
+            'vanillaUid' => 23,
+            'neighborRow' => null,
+            'inlineChildChildUid' => null,
+            'databaseRow' => [],
+            'isInlineChild' => true,
+            'inlineParentUid' => 42,
+            'inlineParentConfig' => [
+                'foreign_field' => 'theParentField'
+             ],
+        ];
+        $expected = $input;
+        $expected['databaseRow']['theParentField'] = 42;
+        $expected['databaseRow']['pid'] = 23;
+        self::assertSame($expected, (new DatabaseRowInitializeNew)->addData($input));
+    }
 }
-- 
GitLab