Commit d29d4b5f authored by Christian Kuhn's avatar Christian Kuhn Committed by Stefan Neufeind
Browse files

[TASK] Free FormEngine from _GP usage

Solves a todo in FormEngine: A data provider still
fetched data from _GP instead of relying of this data
being set by a controller: "defaultValues" for new
records can now be set by controllers as input $result
array key.

Resolves: #84506
Releases: master
Change-Id: I174eea2d5954b7ac29edef1007c1446c7816b0ed
Reviewed-on: https://review.typo3.org/56390

Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: default avatarStefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: default avatarStefan Neufeind <typo3.neufeind@speedpartner.de>
parent 0c90a6e1
......@@ -548,6 +548,7 @@ class EditDocumentController
$this->uc = $parsedBody['uc'] ?? $queryParams['uc'] ?? null;
// Set overrideVals as default values if defVals does not exist.
// @todo: Why?
if (!is_array($this->defVals) && is_array($this->overrideVals)) {
$this->defVals = $this->overrideVals;
}
......@@ -1221,6 +1222,9 @@ class EditDocumentController
if (is_array($this->overrideVals) && is_array($this->overrideVals[$table])) {
$formDataCompilerInput['overrideValues'] = $this->overrideVals[$table];
}
if (!empty($this->defVals) && is_array($this->defVals)) {
$formDataCompilerInput['defaultValues'] = $this->defVals;
}
$formData = $formDataCompiler->compile($formDataCompilerInput);
......
......@@ -291,9 +291,15 @@ class FormDataCompiler
'tabAndInlineStack' => [],
'inlineData' => [],
'inlineStructure' => [],
// This array of fields will be set as hidden-fields instead of rendered normally!
// This is used by EditDocumentController to force some field values if set as "overrideVals" in _GP
'overrideValues' => [],
// Default values for fields. This is for example used in DatabaseRowInitializeNew data provider to set
// fields to specific values if new records are created. Values are often carried around as "defVals" GET
// parameters and hand over by controllers to FormEngine using this array. Array structure is
// ['aTableName']['aFieldName'] = 'aValue'.
'defaultValues' => [],
// This array must NOT be set / manipulated by data providers but is instead used by the render part
// of FormEngine to add runtime data. Containers and elements add data here which is given to
......
......@@ -46,7 +46,7 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface
$result = $this->setDefaultsFromUserTsConfig($result);
$result = $this->setDefaultsFromPageTsConfig($result);
$result = $this->setDefaultsFromNeighborRow($result);
$result = $this->setDefaultsFromDevVals($result);
$result = $this->setDefaultsFromDefaultValues($result);
$result = $this->setDefaultsFromInlineRelations($result);
$result = $this->setDefaultsFromInlineParentLanguage($result);
$result = $this->setPid($result);
......@@ -122,23 +122,19 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface
}
/**
* Apply default values from GET / POST
*
* @todo: Fetch this stuff from request object as soon as modules were moved to PSR-7,
* @todo: or hand values over via $result array, so the _GP access is transferred to
* @todo: controllers concern.
* Apply default values.
* These are typically carried around as "defVals" GET vars and set by controllers
* in $result['defaultValues'] array as init values.
*
* @param array $result Result array
* @return array Modified result array
*/
protected function setDefaultsFromDevVals(array $result)
protected function setDefaultsFromDefaultValues(array $result)
{
$tableName = $result['tableName'];
$defaultValuesFromGetPost = GeneralUtility::_GP('defVals');
if (isset($defaultValuesFromGetPost[$tableName])
&& is_array($defaultValuesFromGetPost[$tableName])
) {
foreach ($defaultValuesFromGetPost[$tableName] as $fieldName => $fieldValue) {
$defaultValues = $result['defaultValues'] ?? [];
if (isset($defaultValues[$tableName]) && is_array($defaultValues[$tableName])) {
foreach ($defaultValues[$tableName] as $fieldName => $fieldValue) {
if (isset($result['processedTca']['columns'][$fieldName])) {
$result['databaseRow'][$fieldName] = $fieldValue;
}
......
......@@ -333,7 +333,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
/**
* @test
*/
public function addDataSetsDefaultDataFromGetIfColumnIsDefinedInTca()
public function addDataSetsDefaultDataFromDefaultValuesIfColumnIsDefinedInTca()
{
$input = [
'command' => 'new',
......@@ -347,92 +347,15 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
'columns' => [
'aField' => [],
],
]
];
$GLOBALS['_GET'] = [
'defVals' => [
'aTable' => [
'aField' => 'getValue',
],
],
];
$expected = [
'aField' => 'getValue',
'pid' => 23,
];
$result = (new DatabaseRowInitializeNew)->addData($input);
$this->assertSame($expected, $result['databaseRow']);
}
/**
* @test
*/
public function addDataSetsDefaultDataFromPostIfColumnIsDefinedInTca()
{
$input = [
'command' => 'new',
'tableName' => 'aTable',
'vanillaUid' => 23,
'neighborRow' => null,
'inlineChildChildUid' => null,
'isInlineChild' => false,
'databaseRow' => [],
'processedTca' => [
'columns' => [
'aField' => [],
],
]
];
$GLOBALS['_POST'] = [
'defVals' => [
'aTable' => [
'aField' => 'postValue',
],
],
];
$expected = [
'aField' => 'postValue',
'pid' => 23,
];
$result = (new DatabaseRowInitializeNew)->addData($input);
$this->assertSame($expected, $result['databaseRow']);
}
/**
* @test
*/
public function addDataSetsPrioritizesDefaultPostOverDefaultGet()
{
$input = [
'command' => 'new',
'tableName' => 'aTable',
'vanillaUid' => 23,
'neighborRow' => null,
'inlineChildChildUid' => null,
'isInlineChild' => false,
'databaseRow' => [],
'processedTca' => [
'columns' => [
'aField' => [],
],
]
];
$GLOBALS['_GET'] = [
'defVals' => [
'defaultValues' => [
'aTable' => [
'aField' => 'getValue',
],
],
];
$GLOBALS['_POST'] = [
'defVals' => [
'aTable' => [
'aField' => 'postValue',
],
],
];
$expected = [
'aField' => 'postValue',
'aField' => 'getValue',
'pid' => 23,
];
$result = (new DatabaseRowInitializeNew)->addData($input);
......@@ -442,7 +365,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
/**
* @test
*/
public function addDataDoesNotSetDefaultDataFromGetPostIfColumnIsMissingInTca()
public function addDataDoesNotSetDefaultDataFromDefaultValuesIfColumnIsMissingInTca()
{
$input = [
'command' => 'new',
......@@ -461,19 +384,10 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
],
'processedTca' => [
'columns' => [],
]
];
$GLOBALS['_GET'] = [
'defVals' => [
'aTable' => [
'aField' => 'getValue',
],
],
];
$GLOBALS['_POST'] = [
'defVals' => [
'defaultValues' => [
'aTable' => [
'aField' => 'postValue',
'aField' => 'getValue',
],
],
];
......@@ -487,7 +401,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
/**
* @test
*/
public function addDataSetsDefaultDataOverrulingGetPost()
public function addDataSetsDefaultDataOverrulesOtherDefauls()
{
$input = [
'command' => 'new',
......@@ -521,13 +435,11 @@ class DatabaseRowInitializeNewTest extends UnitTestCase
'aField' => [],
],
],
];
$GLOBALS['_POST'] = [
'defVals' => [
'defaultValues' => [
'aTable' => [
'aField' => 'postValue',
],
],
]
];
$expected = [
'aField' => 'postValue',
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment