Skip to content

[Form] Keep preferred_choices order for choice groups #34083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
unset($otherViews[$key]);
}
}

foreach ($preferredViewsOrder as $key => $groupViewsOrder) {
if ($groupViewsOrder) {
$preferredViewsOrder[$key] = min($groupViewsOrder);
} else {
unset($preferredViewsOrder[$key]);
}
}
} else {
// Otherwise use the original structure of the choices
self::addChoiceViewsFromStructuredValues(
Expand Down Expand Up @@ -245,6 +253,9 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
$preferredViews[$groupLabel] = new ChoiceGroupView($groupLabel);
$otherViews[$groupLabel] = new ChoiceGroupView($groupLabel);
}
if (!isset($preferredViewsOrder[$groupLabel])) {
$preferredViewsOrder[$groupLabel] = [];
}

self::addChoiceView(
$choice,
Expand All @@ -255,7 +266,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
$attr,
$isPreferred,
$preferredViews[$groupLabel]->choices,
$preferredViewsOrder,
$preferredViewsOrder[$groupLabel],
$otherViews[$groupLabel]->choices
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,37 @@ public function testCreateViewFlatPreferredChoicesSameOrder()
);
}

public function testCreateViewFlatPreferredChoiceGroupsSameOrder()
{
$view = $this->factory->createView(
$this->list,
[$this->obj4, $this->obj2, $this->obj1, $this->obj3],
null, // label
null, // index
[$this, 'getGroup']
);

$preferredLabels = array_map(static function (ChoiceGroupView $groupView): array {
return array_map(static function (ChoiceView $view): string {
return $view->label;
}, $groupView->choices);
}, $view->preferredChoices);

$this->assertEquals(
[
'Group 2' => [
2 => 'C',
3 => 'D',
],
'Group 1' => [
0 => 'A',
1 => 'B',
],
],
$preferredLabels
);
}

public function testCreateViewFlatPreferredChoicesEmptyArray()
{
$view = $this->factory->createView(
Expand Down