Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b15c028

Browse files
committed
Keep preferred_choices order
1 parent a36fbe3 commit b15c028

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
4848
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
4949
{
5050
$preferredViews = [];
51+
$preferredViewsOrder = [];
5152
$otherViews = [];
5253
$choices = $list->getChoices();
5354
$keys = $list->getOriginalKeys();
5455

5556
if (!\is_callable($preferredChoices) && !empty($preferredChoices)) {
56-
$preferredChoices = function ($choice) use ($preferredChoices) {
57-
return \in_array($choice, $preferredChoices, true);
57+
// make sure we have keys that reflect order
58+
$preferredChoices = array_values($preferredChoices);
59+
$preferredChoices = static function ($choice) use ($preferredChoices) {
60+
return array_search($choice, $preferredChoices, true);
5861
};
5962
}
6063

@@ -80,6 +83,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
8083
$attr,
8184
$preferredChoices,
8285
$preferredViews,
86+
$preferredViewsOrder,
8387
$otherViews
8488
);
8589
}
@@ -108,14 +112,21 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
108112
$attr,
109113
$preferredChoices,
110114
$preferredViews,
115+
$preferredViewsOrder,
111116
$otherViews
112117
);
113118
}
114119

120+
uksort($preferredViews, static function ($a, $b) use ($preferredViewsOrder): int {
121+
return isset($preferredViewsOrder[$a], $preferredViewsOrder[$b])
122+
? $preferredViewsOrder[$a] <=> $preferredViewsOrder[$b]
123+
: 0;
124+
});
125+
115126
return new ChoiceListView($otherViews, $preferredViews);
116127
}
117128

118-
private static function addChoiceView($choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
129+
private static function addChoiceView($choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews)
119130
{
120131
// $value may be an integer or a string, since it's stored in the array
121132
// keys. We want to guarantee it's a string though.
@@ -143,14 +154,15 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
143154
);
144155

145156
// $isPreferred may be null if no choices are preferred
146-
if ($isPreferred && $isPreferred($choice, $key, $value)) {
157+
if ($isPreferred && false !== $preferredKey = $isPreferred($choice, $key, $value)) {
147158
$preferredViews[$nextIndex] = $view;
159+
$preferredViewsOrder[$nextIndex] = $preferredKey;
148160
} else {
149161
$otherViews[$nextIndex] = $view;
150162
}
151163
}
152164

153-
private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
165+
private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews)
154166
{
155167
foreach ($values as $key => $value) {
156168
if (null === $value) {
@@ -171,6 +183,7 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho
171183
$attr,
172184
$isPreferred,
173185
$preferredViewsForGroup,
186+
$preferredViewsOrder,
174187
$otherViewsForGroup
175188
);
176189

@@ -195,12 +208,13 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho
195208
$attr,
196209
$isPreferred,
197210
$preferredViews,
211+
$preferredViewsOrder,
198212
$otherViews
199213
);
200214
}
201215
}
202216

203-
private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
217+
private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews)
204218
{
205219
$groupLabels = $groupBy($choice, $keys[$value], $value);
206220

@@ -215,6 +229,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
215229
$attr,
216230
$isPreferred,
217231
$preferredViews,
232+
$preferredViewsOrder,
218233
$otherViews
219234
);
220235

@@ -240,6 +255,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
240255
$attr,
241256
$isPreferred,
242257
$preferredViews[$groupLabel]->choices,
258+
$preferredViewsOrder,
243259
$otherViews[$groupLabel]->choices
244260
);
245261
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,33 @@ public function testCreateViewFlatPreferredChoices()
234234
$this->assertFlatView($view);
235235
}
236236

237+
public function testCreateViewFlatPreferredChoicesKeepOrder()
238+
{
239+
$view = $this->factory->createView(
240+
$this->list,
241+
[$this->obj2, $this->obj1, $this->obj4, $this->obj3],
242+
null,
243+
null,
244+
null,
245+
null,
246+
true // $keepPreferredChoicesOrder
247+
);
248+
249+
$preferredLabels = array_map(static function (ChoiceView $view) {
250+
return $view->label;
251+
}, $view->preferredChoices);
252+
253+
$this->assertSame(
254+
[
255+
1 => 'B',
256+
0 => 'A',
257+
3 => 'D',
258+
2 => 'C',
259+
],
260+
$preferredLabels
261+
);
262+
}
263+
237264
public function testCreateViewFlatPreferredChoicesEmptyArray()
238265
{
239266
$view = $this->factory->createView(

0 commit comments

Comments
 (0)