Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 97ed3bf

Browse filesBrowse files
committed
Keep preferred_choices order for choice groups
1 parent 274ff66 commit 97ed3bf
Copy full SHA for 97ed3bf

File tree

2 files changed

+45
-1
lines changed
Filter options

2 files changed

+45
-1
lines changed

‎src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
101101
unset($otherViews[$key]);
102102
}
103103
}
104+
105+
foreach ($preferredViewsOrder as $key => $groupViewsOrder) {
106+
if (\is_array($groupViewsOrder)) {
107+
if (0 === \count($groupViewsOrder)) {
108+
unset($preferredViewsOrder[$key]);
109+
} else {
110+
$preferredViewsOrder[$key] = \min($groupViewsOrder);
111+
}
112+
}
113+
}
104114
} else {
105115
// Otherwise use the original structure of the choices
106116
self::addChoiceViewsFromStructuredValues(
@@ -245,6 +255,9 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
245255
$preferredViews[$groupLabel] = new ChoiceGroupView($groupLabel);
246256
$otherViews[$groupLabel] = new ChoiceGroupView($groupLabel);
247257
}
258+
if (!isset($preferredViewsOrder[$groupLabel])) {
259+
$preferredViewsOrder[$groupLabel] = [];
260+
}
248261

249262
self::addChoiceView(
250263
$choice,
@@ -255,7 +268,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
255268
$attr,
256269
$isPreferred,
257270
$preferredViews[$groupLabel]->choices,
258-
$preferredViewsOrder,
271+
$preferredViewsOrder[$groupLabel],
259272
$otherViews[$groupLabel]->choices
260273
);
261274
}

‎src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,37 @@ public function testCreateViewFlatPreferredChoicesSameOrder()
256256
);
257257
}
258258

259+
public function testCreateViewFlatPreferredChoiceGroupsSameOrder()
260+
{
261+
$view = $this->factory->createView(
262+
$this->list,
263+
[$this->obj4, $this->obj2, $this->obj1, $this->obj3],
264+
null, // label
265+
null, // index
266+
[$this, 'getGroup']
267+
);
268+
269+
$preferredLabels = array_map(static function (ChoiceGroupView $groupView): array {
270+
return array_map(static function (ChoiceView $view): string {
271+
return $view->label;
272+
}, $groupView->choices);
273+
}, $view->preferredChoices);
274+
275+
$this->assertEquals(
276+
[
277+
'Group 2' => [
278+
2 => 'C',
279+
3 => 'D',
280+
],
281+
'Group 1' => [
282+
0 => 'A',
283+
1 => 'B',
284+
],
285+
],
286+
$preferredLabels
287+
);
288+
}
289+
259290
public function testCreateViewFlatPreferredChoicesEmptyArray()
260291
{
261292
$view = $this->factory->createView(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.