From 408f9bcce837cfe4c393e7efaca2a97048cd927e Mon Sep 17 00:00:00 2001
From: Georg Ringer <georg.ringer@gmail.com>
Date: Sat, 7 Mar 2020 01:36:53 +0100
Subject: [PATCH] [BUGFIX] Fetch correct uid for ###REC_FIELD and group fields

If a field is used in `foreign_table_where` configuration which
points to a field using `group` as type, the uid needs to be fetched
differently from the processed row.

Resolves: #17073
Releases: master, 9.5
Change-Id: I8ec9c7cd0dd166a0c4e87a61adb2f87b7c1c8cf5
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63595
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <look@susi.dev>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Susanne Moog <look@susi.dev>
---
 .../Classes/Form/FormDataProvider/AbstractItemProvider.php  | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php
index 8991c6a7ad30..2a2fba654e36 100644
--- a/typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php
+++ b/typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php
@@ -1101,8 +1101,10 @@ abstract class AbstractItemProvider
                         $rowFieldValue = $result[$databaseRowKey][$whereClauseSubParts[0]] ?? '';
                         if (is_array($rowFieldValue)) {
                             // If a select or group field is used here, it may have been processed already and
-                            // is now an array. Use first selected value in this case.
-                            $rowFieldValue = $rowFieldValue[0];
+                            // is now an array containing uid + table + title + row.
+                            // See TcaGroup data provider for details.
+                            // Pick the first one (always on 0), and use uid only.
+                            $rowFieldValue = $rowFieldValue[0]['uid'] ?? $rowFieldValue[0];
                         }
                         if (substr($whereClauseParts[0], -1) === '\'' && $whereClauseSubParts[1][0] === '\'') {
                             $whereClauseParts[0] = substr($whereClauseParts[0], 0, -1);
-- 
GitLab