Use Form getSaveData method when saving form widgets within repeaters (#4160)

Credit to @bennothommo
This commit is contained in:
Ben Thomson 2019-03-16 12:39:34 +08:00 committed by Luke Towers
parent 13d710b161
commit 13a7cc915d
2 changed files with 4 additions and 89 deletions

View File

@ -202,15 +202,9 @@ class Repeater extends FormWidgetBase
/*
* Give repeated form field widgets an opportunity to process the data.
*/
foreach ($this->formWidgets as $field => $form) {
foreach ($form->getFormWidgets() as $formField => $widget) {
$parts = HtmlHelper::nameToArray($field . '[' . $formField . ']');
$widgetValue = $widget->getSaveValue($this->dataArrayGet($value, $parts));
if (empty($widgetValue) || !count($widgetValue)) {
continue;
}
$this->dataArraySet($value, $parts, $widgetValue);
foreach ($value as $index => $data) {
if (isset($this->formWidgets[$index])) {
$value[$index] = $this->formWidgets[$index]->getSaveData();
}
}
@ -421,83 +415,4 @@ class Repeater extends FormWidgetBase
{
return array_get($this->groupDefinitions, $groupCode.'.name');
}
/**
* Internal helper for method existence checks.
*
* @param object $object
* @param string $method
* @return boolean
*/
protected function objectMethodExists($object, $method)
{
if (method_exists($object, 'methodExists')) {
return $object->methodExists($method);
}
return method_exists($object, $method);
}
/**
* Variant to array_get() but preserves dots in key names.
*
* @param array $array
* @param array $parts
* @param null $default
* @return array|null
*/
protected function dataArrayGet(array $array, array $parts, $default = null)
{
if ($parts === null) {
return $array;
}
if (count($parts) === 1) {
$key = array_shift($parts);
if (isset($array[$key])) {
return $array[$key];
}
return $default;
}
foreach ($parts as $segment) {
if (!is_array($array) || !array_key_exists($segment, $array)) {
return $default;
}
$array = $array[$segment];
}
return $array;
}
/**
* Variant to array_set() but preserves dots in key names.
*
* @param array $array
* @param array $parts
* @param string $value
* @return array
*/
protected function dataArraySet(array &$array, array $parts, $value)
{
if ($parts === null) {
return $value;
}
while (count($parts) > 1) {
$key = array_shift($parts);
if (!isset($array[$key]) || !is_array($array[$key])) {
$array[$key] = [];
}
$array =& $array[$key];
}
$array[array_shift($parts)] = $value;
return $array;
}
}

View File

@ -1076,7 +1076,7 @@ class Form extends WidgetBase
: null;
return $field->getValueFromData(
$this->data,
$this->data,
is_string($defaultValue) ? trans($defaultValue) : $defaultValue
);
}