Fix group index mapping in Repeater widget (#4334)

Fixes a bug which causes grouped repeaters to lose data when repeater items are re-ordered and saved, due to the data indexes not being correctly mapped to the corresponding form widget.

Credit to @datune.
This commit is contained in:
datune 2019-05-14 17:34:12 +02:00 committed by Ben Thomson
parent 4e92686c1a
commit a8d9dcf985
1 changed files with 8 additions and 15 deletions

View File

@ -217,9 +217,7 @@ class Repeater extends FormWidgetBase
$this->indexCount = 0;
$this->formWidgets = [];
return;
}
$groupMap = [];
}
// Ensure that the minimum number of items are preinitialized
// ONLY DONE WHEN NOT IN GROUP MODE
@ -235,20 +233,15 @@ class Repeater extends FormWidgetBase
}
}
}
if (is_array($currentValue) && count($currentValue)) {
foreach ($currentValue as $value) {
$groupMap[] = array_get($value, '_group');
}
}
if (!count($groupMap)) {
if (!is_array($currentValue)) {
return;
}
foreach ($groupMap as $index => $groupCode) {
$this->makeItemFormWidget($index, $groupCode);
}
collect($currentValue)->each(function ($value, $index) {
$this->makeItemFormWidget($index, array_get($value, '_group', null));
});
$this->indexCount = max(count($currentValue), $this->indexCount);
}