From 494e37db93a6f201fe6e0aad6e4a8e7118c68cdd Mon Sep 17 00:00:00 2001
From: Frans Saris <franssaris@gmail.com>
Date: Fri, 2 Oct 2015 11:44:24 +0200
Subject: [PATCH] [BUGFIX] Fetch correct recordTypeValue for group fields

Change-Id: I4609e84720339818b3a978c864c00b3fc516bcf5
Resolves: #70301
Releases: master
Reviewed-on: http://review.typo3.org/43734
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
---
 .../DatabaseRecordTypeValue.php               |  7 +++
 .../DatabaseRecordTypeValueTest.php           | 45 +++++++++++++++++++
 .../Configuration/DefaultConfiguration.php    |  2 +
 3 files changed, 54 insertions(+)

diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRecordTypeValue.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRecordTypeValue.php
index 60116e5c5f4b..5506b3f5bf6d 100644
--- a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRecordTypeValue.php
+++ b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRecordTypeValue.php
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Backend\Form\FormDataProvider;
 
 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Utility\MathUtility;
 
 /**
  * Determine the final TCA type value
@@ -91,6 +92,12 @@ class DatabaseRecordTypeValue implements FormDataProviderInterface {
 							1438253614
 						);
 					}
+					// Extract UID from value formed like {table_name}_{uid}|{default_value}
+					// @todo: This needs adaption as soon as the group format is changed
+					if (!MathUtility::canBeInterpretedAsInteger($foreignUid)) {
+						list($foreignUid) = explode('|', $foreignUid);
+						$foreignUid = str_replace($foreignTable . '_', '', $foreignUid);
+					}
 					// Fetch field of this foreign row from db
 					$foreignRow = BackendUtility::getRecord($foreignTable, $foreignUid, $foreignTableTypeField);
 					if ($foreignRow[$foreignTableTypeField]) {
diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRecordTypeValueTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRecordTypeValueTest.php
index c51e157ef452..bf827596e38d 100644
--- a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRecordTypeValueTest.php
+++ b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRecordTypeValueTest.php
@@ -409,4 +409,49 @@ class DatabaseRecordTypeValueTest extends UnitTestCase {
 		$this->assertSame($expected, $this->subject->addData($input));
 	}
 
+
+	/**
+	 * @test
+	 */
+	public function addDataSetsTypeValueFromNestedTcaGroupField() {
+		$input = [
+			'vanillaTableTca' => [
+				'ctrl' => [
+					'type' => 'uid_local:type',
+				],
+				'columns' => [
+					'uid_local' => [
+						'config' => [
+							'type' => 'group',
+							'internal_type' => 'db',
+							'size' => 1,
+							'maxitems' => 1,
+							'minitems' => 0,
+							'allowed' => 'sys_file'
+						],
+					],
+				],
+				'types' => [
+					'2' => 'foo',
+				],
+			],
+			'databaseRow' => [
+				// Processed group field
+				'uid_local' => 'sys_file_222|my_test.jpg',
+			],
+		];
+
+		$foreignRecordResult = [
+			'type' => 2,
+		];
+		// Required for BackendUtility::getRecord
+		$GLOBALS['TCA']['sys_file'] = array('foo');
+
+		$this->dbProphecy->exec_SELECTgetSingleRow('type', 'sys_file', 'uid=222')->shouldBeCalled()->willReturn($foreignRecordResult);
+
+		$expected = $input;
+		$expected['recordTypeValue'] = '2';
+
+		$this->assertSame($expected, $this->subject->addData($input));
+	}
 }
diff --git a/typo3/sysext/core/Configuration/DefaultConfiguration.php b/typo3/sysext/core/Configuration/DefaultConfiguration.php
index ddf983bd1e12..661e1a6fe02d 100644
--- a/typo3/sysext/core/Configuration/DefaultConfiguration.php
+++ b/typo3/sysext/core/Configuration/DefaultConfiguration.php
@@ -386,6 +386,8 @@ return array(
 					\TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordTypeValue::class => array(
 						'depends' => array(
 							\TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseLanguageRows::class,
+							// As the ctrl.type can hold a nested key we need to resolve all relations
+							\TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup::class,
 						),
 					),
 					\TYPO3\CMS\Backend\Form\FormDataProvider\PageTsConfigMerged::class => array(
-- 
GitLab