Skip to content

Commit 01be933

Browse files
Merge branch '4.3' into 4.4
* 4.3: Fix tests [Console] Fix commands description with numeric namespaces [HttpFoundation] Fixed typo [EventDispatcher] Better error reporting when arguments to dispatch() are swapped [Serializer] CsvEncoder::NO_HEADERS_KEY ignored when used in constructor [Form] Keep preferred_choices order for choice groups [Debug] work around failing chdir() on Darwin [PhpUnitBridge] Read configuration CLI directive [DI] Missing test on YamlFileLoader Revert "minor #34608 [Process] add tests for php executable finder if file does not exist (ahmedash95)" Simpler example for Apache basic auth workaround [Console] Fix trying to access array offset on value of type int [Config] Remove extra sprintf arg [VarDumper] notice on potential undefined index [HttpClient] turn exception into log when the request has no content-type [Process] add tests for php executable finder if file does not exist [Cache] Make sure we get the correct number of values from redis::mget() [TwigBridge] Add row_attr to all form themes [Serializer] Fix MetadataAwareNameConverter usage with string group
2 parents 5c20bce + dbd10cd commit 01be933

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
105105
unset($otherViews[$key]);
106106
}
107107
}
108+
109+
foreach ($preferredViewsOrder as $key => $groupViewsOrder) {
110+
if ($groupViewsOrder) {
111+
$preferredViewsOrder[$key] = min($groupViewsOrder);
112+
} else {
113+
unset($preferredViewsOrder[$key]);
114+
}
115+
}
108116
} else {
109117
// Otherwise use the original structure of the choices
110118
self::addChoiceViewsFromStructuredValues(
@@ -249,6 +257,9 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
249257
$preferredViews[$groupLabel] = new ChoiceGroupView($groupLabel);
250258
$otherViews[$groupLabel] = new ChoiceGroupView($groupLabel);
251259
}
260+
if (!isset($preferredViewsOrder[$groupLabel])) {
261+
$preferredViewsOrder[$groupLabel] = [];
262+
}
252263

253264
self::addChoiceView(
254265
$choice,
@@ -259,7 +270,7 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
259270
$attr,
260271
$isPreferred,
261272
$preferredViews[$groupLabel]->choices,
262-
$preferredViewsOrder,
273+
$preferredViewsOrder[$groupLabel],
263274
$otherViews[$groupLabel]->choices
264275
);
265276
}

Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines 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)