From 13a7cc915db221b3ca67847fcdaaa4b71972e895 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Sat, 16 Mar 2019 12:39:34 +0800 Subject: [PATCH] Use Form getSaveData method when saving form widgets within repeaters (#4160) Credit to @bennothommo --- modules/backend/formwidgets/Repeater.php | 91 +----------------------- modules/backend/widgets/Form.php | 2 +- 2 files changed, 4 insertions(+), 89 deletions(-) diff --git a/modules/backend/formwidgets/Repeater.php b/modules/backend/formwidgets/Repeater.php index 5a4328972..35088a670 100644 --- a/modules/backend/formwidgets/Repeater.php +++ b/modules/backend/formwidgets/Repeater.php @@ -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; - } } diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index b7362cab3..d29c67c3d 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -1076,7 +1076,7 @@ class Form extends WidgetBase : null; return $field->getValueFromData( - $this->data, + $this->data, is_string($defaultValue) ? trans($defaultValue) : $defaultValue ); }