Skip to content

Commit

Permalink
[BUGFIX] Do not write empty array values in form definitions
Browse files Browse the repository at this point in the history
Symfony YAML dumps empty arrays as sequence ("{  }"). Due to this, some
JavaScript modules of the form framework fail to interpret/ set the
correct data type for those properties.

If applied, this commit will ensure that the formEditor unsets empty
array keys on load/ save.

The problem only affects the backend module, not the frontend
formDefinition interpreter.

Resolves: #83985
Releases: master, 8.7
Change-Id: I913b8954790510b4ffcc80a47f9dbb7646239ff1
Reviewed-on: https://review.typo3.org/55848
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Björn Jacob <bjoern.jacob@tritum.de>
Tested-by: Björn Jacob <bjoern.jacob@tritum.de>
Reviewed-by: Kay Strobach <typo3@kay-strobach.de>
Tested-by: Kay Strobach <typo3@kay-strobach.de>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-on: https://review.typo3.org/56744
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
  • Loading branch information
waldhacker1 authored and ohader committed Apr 19, 2018
1 parent 9fc37bd commit ca58248
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
31 changes: 29 additions & 2 deletions typo3/sysext/form/Classes/Controller/FormEditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function indexAction(string $formPersistenceIdentifier, string $prototype
$prototypeName = isset($formDefinition['prototypeName']) ? $formDefinition['prototypeName'] : 'standard';
}
$formDefinition['prototypeName'] = $prototypeName;
$formDefinition = $this->filterEmptyArrays($formDefinition);

$configurationService = $this->objectManager->get(ConfigurationService::class);
$this->prototypeConfiguration = $configurationService->getPrototypeConfiguration($prototypeName);
Expand Down Expand Up @@ -153,6 +154,7 @@ public function initializeSaveFormAction()
public function saveFormAction(string $formPersistenceIdentifier, FormDefinitionArray $formDefinition)
{
$formDefinition = $formDefinition->getArrayCopy();
$formDefinition = $this->filterEmptyArrays($formDefinition);
if (
isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeFormSave'])
&& is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeFormSave'])
Expand Down Expand Up @@ -217,7 +219,7 @@ public function renderFormPageAction(FormDefinitionArray $formDefinition, int $p
}

/**
* Prepare the formElements.*.formEditor section from the yaml settings.
* Prepare the formElements.*.formEditor section from the YAML settings.
* Sort all formElements into groups and add additional data.
*
* @param array $formElementsDefinition
Expand Down Expand Up @@ -277,7 +279,7 @@ protected function getInsertRenderablesPanelConfiguration(array $formElementsDef
}

/**
* Reduce the Yaml settings by the 'formEditor' keyword.
* Reduce the YAML settings by the 'formEditor' keyword.
*
* @return array
*/
Expand Down Expand Up @@ -519,6 +521,31 @@ protected function transformMultiValueElementsForFormEditor(
return $output;
}

/**
* Remove keys from an array if the key value is an empty array
*
* @param array $array
* @return array
*/
protected function filterEmptyArrays(array $array): array
{
foreach ($array as $key => $value) {
if (!is_array($value)) {
continue;
}
if (empty($value)) {
unset($array[$key]);
continue;
}
$array[$key] = $this->filterEmptyArrays($value);
if (empty($array[$key])) {
unset($array[$key]);
}
}

return $array;
}

/**
* Returns the current BE user.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1425,9 +1425,8 @@ define(['jquery',
var that = $(this);
if (!$.isNumeric(that.val())) {
that.val('');
} else {
getCurrentlySelectedFormElement().set(propertyPath, that.val());
}
getCurrentlySelectedFormElement().set(propertyPath, that.val());
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,39 @@ public function transformMultiValueElementsForFormEditorConvertMultiValueDataInt

$this->assertSame($expected, $mockController->_call('transformMultiValueElementsForFormEditor', $input, $multiValueProperties));
}

/**
* @test
*/
public function filterEmptyArraysRemovesEmptyArrayKeys()
{
$mockController = $this->getAccessibleMock(FormEditorController::class, [
'dummy'
], [], '', false);

$input = [
'heinz' => 1,
'klaus' => [],
'sabine' => [
'heinz' => '2',
'klaus' => [],
'horst' => [
'heinz' => '',
'paul' => [[]],
],
],
];

$expected = [
'heinz' => 1,
'sabine' => [
'heinz' => '2',
'horst' => [
'heinz' => '',
],
],
];

$this->assertSame($expected, $mockController->_call('filterEmptyArrays', $input));
}
}

0 comments on commit ca58248

Please sign in to comment.