diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php index 348629f863cba88c514d7c247937260eb587b7ac..6e8c7e2ccffa2a0f12e145b2e2548c24ff2f526d 100644 --- a/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php +++ b/typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php @@ -16,6 +16,7 @@ namespace TYPO3\CMS\Backend\Form\FormDataProvider; use TYPO3\CMS\Backend\Form\FormDataProviderInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Core\Utility\MathUtility; /** * On "new" command, initialize new database row with default data @@ -47,11 +48,7 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface $result = $this->setDefaultsFromNeighborRow($result); $result = $this->setDefaultsFromDevVals($result); $result = $this->setDefaultsFromInlineRelations($result); - - // Set pid to vanillaUid. This means, it *can* be negative, if the record is added relative to another record - // @todo: For inline records it should be possible to set the pid here via TCAdefaults, but - // @todo: those values would be overwritten by this 'pid' setter - $result['databaseRow']['pid'] = $result['vanillaUid']; + $result = $this->setPid($result); return $result; } @@ -194,4 +191,36 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface return $result; } + + /** + * Set the pid. This is either the vanillaUid (see description in FormDataCompiler), + * or a pid given by pageTsConfig for inline children. + * + * @param array $result Result array + * @return array Modified result array + * @throws \UnexpectedValueException + */ + protected function setPid(array $result) + { + // Set pid to vanillaUid. This can be a negative value + // if the record is added relative to another record. + $result['databaseRow']['pid'] = $result['vanillaUid']; + + // In case a new inline record is created, the pid can be set to a different value + // by pageTsConfig, but not by userTsConfig. This overrides the above pid selection + // and forces the pid of new inline children. + $tableNameWithDot = $result['tableName'] . '.'; + if ($result['isInlineChild'] && isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) { + if (!MathUtility::canBeInterpretedAsInteger($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) { + throw new \UnexpectedValueException( + 'page TSConfig setting TCAdefaults.' . $tableNameWithDot . 'pid must be a number, but given string ' + . $result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'] . ' can not be interpreted as integer', + 1461598332 + ); + } + $result['databaseRow']['pid'] = (int)$result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid']; + } + + return $result; + } } diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php index cf7f347d91557cc016e6ba068399206a6aff840e..5af0e0919af760e879540d8147e2e36b205bbf1b 100644 --- a/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php +++ b/typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php @@ -64,7 +64,8 @@ class DatabaseRowInitializeNewTest extends UnitTestCase 'command' => 'new', 'databaseRow' => 'not-an-array', ]; - $this->setExpectedException(\UnexpectedValueException::class, '', 1444431128); + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionCode(1444431128); $this->subject->addData($input); } @@ -89,7 +90,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase /** * @test */ - public function addDataSetsDefaultDataFormUserTsIfColumnIsDenfinedInTca() + public function addDataSetsDefaultDataFormUserTsIfColumnIsDefinedInTca() { $input = [ 'command' => 'new', @@ -148,7 +149,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase /** * @test */ - public function addDataSetsDefaultDataFormPageTsIfColumnIsDenfinedInTca() + public function addDataSetsDefaultDataFormPageTsIfColumnIsDefinedInTca() { $input = [ 'command' => 'new', @@ -350,7 +351,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase /** * @test */ - public function addDataSetsDefaultDataFormPostIfColumnIsDenfinedInTca() + public function addDataSetsDefaultDataFromPostIfColumnIsDenfinedInTca() { $input = [ 'command' => 'new', @@ -519,7 +520,8 @@ class DatabaseRowInitializeNewTest extends UnitTestCase 'databaseRow' => [], 'inlineChildChildUid' => 42, ]; - $this->setExpectedException(\UnexpectedValueException::class, '', 1444434102); + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionCode(1444434102); $this->subject->addData($input); } @@ -533,7 +535,8 @@ class DatabaseRowInitializeNewTest extends UnitTestCase 'databaseRow' => [], 'inlineChildChildUid' => '42', ]; - $this->setExpectedException(\UnexpectedValueException::class, '', 1444434103); + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionCode(1444434103); $this->subject->addData($input); } @@ -589,7 +592,8 @@ class DatabaseRowInitializeNewTest extends UnitTestCase ], ], ]; - $this->setExpectedException(\UnexpectedValueException::class, '', 1444434104); + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionCode(1444434104); $this->subject->addData($input); } @@ -608,4 +612,76 @@ class DatabaseRowInitializeNewTest extends UnitTestCase $result = $this->subject->addData($input); $this->assertSame($expected, $result['databaseRow']); } + + /** + * @test + */ + public function addDataDoesNotUsePageTsValueForPidIfRecordIsNotInlineChild() + { + $input = [ + 'command' => 'new', + 'tableName' => 'aTable', + 'vanillaUid' => 23, + 'databaseRow' => [], + 'pageTsConfig' => [ + 'TCAdefaults.' => [ + 'aTable.' => [ + 'pid' => '42', + ], + ], + ], + 'isInlineChild' => false, + ]; + $expected = $input; + $expected['databaseRow']['pid'] = 23; + $this->assertSame($expected, $this->subject->addData($input)); + } + + /** + * @test + */ + public function addDataThrowsExceptionIfPageTsConfigPidValueCanNotBeInterpretedAsInteger() + { + $input = [ + 'command' => 'new', + 'tableName' => 'aTable', + 'vanillaUid' => 23, + 'databaseRow' => [], + 'pageTsConfig' => [ + 'TCAdefaults.' => [ + 'aTable.' => [ + 'pid' => 'notAnInteger', + ], + ], + ], + 'isInlineChild' => true, + ]; + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionCode(1461598332); + $this->subject->addData($input); + } + + /** + * @test + */ + public function addDataDoesUsePageTsValueForPidIfRecordIsInlineChild() + { + $input = [ + 'command' => 'new', + 'tableName' => 'aTable', + 'vanillaUid' => 23, + 'databaseRow' => [], + 'pageTsConfig' => [ + 'TCAdefaults.' => [ + 'aTable.' => [ + 'pid' => '42', + ], + ], + ], + 'isInlineChild' => true, + ]; + $expected = $input; + $expected['databaseRow']['pid'] = 42; + $this->assertSame($expected, $this->subject->addData($input)); + } }