@@ -48,13 +48,16 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
48
48
public function createView (ChoiceListInterface $ list , $ preferredChoices = null , $ label = null , $ index = null , $ groupBy = null , $ attr = null )
49
49
{
50
50
$ preferredViews = [];
51
+ $ preferredViewsOrder = [];
51
52
$ otherViews = [];
52
53
$ choices = $ list ->getChoices ();
53
54
$ keys = $ list ->getOriginalKeys ();
54
55
55
56
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 );
58
61
};
59
62
}
60
63
@@ -80,6 +83,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
80
83
$ attr ,
81
84
$ preferredChoices ,
82
85
$ preferredViews ,
86
+ $ preferredViewsOrder ,
83
87
$ otherViews
84
88
);
85
89
}
@@ -108,14 +112,21 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
108
112
$ attr ,
109
113
$ preferredChoices ,
110
114
$ preferredViews ,
115
+ $ preferredViewsOrder ,
111
116
$ otherViews
112
117
);
113
118
}
114
119
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
+
115
126
return new ChoiceListView ($ otherViews , $ preferredViews );
116
127
}
117
128
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 )
119
130
{
120
131
// $value may be an integer or a string, since it's stored in the array
121
132
// keys. We want to guarantee it's a string though.
@@ -143,14 +154,15 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
143
154
);
144
155
145
156
// $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 )) {
147
158
$ preferredViews [$ nextIndex ] = $ view ;
159
+ $ preferredViewsOrder [$ nextIndex ] = $ preferredKey ;
148
160
} else {
149
161
$ otherViews [$ nextIndex ] = $ view ;
150
162
}
151
163
}
152
164
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 )
154
166
{
155
167
foreach ($ values as $ key => $ value ) {
156
168
if (null === $ value ) {
@@ -171,6 +183,7 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho
171
183
$ attr ,
172
184
$ isPreferred ,
173
185
$ preferredViewsForGroup ,
186
+ $ preferredViewsOrder ,
174
187
$ otherViewsForGroup
175
188
);
176
189
@@ -195,12 +208,13 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho
195
208
$ attr ,
196
209
$ isPreferred ,
197
210
$ preferredViews ,
211
+ $ preferredViewsOrder ,
198
212
$ otherViews
199
213
);
200
214
}
201
215
}
202
216
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 )
204
218
{
205
219
$ groupLabels = $ groupBy ($ choice , $ keys [$ value ], $ value );
206
220
@@ -215,6 +229,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
215
229
$ attr ,
216
230
$ isPreferred ,
217
231
$ preferredViews ,
232
+ $ preferredViewsOrder ,
218
233
$ otherViews
219
234
);
220
235
@@ -240,6 +255,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
240
255
$ attr ,
241
256
$ isPreferred ,
242
257
$ preferredViews [$ groupLabel ]->choices ,
258
+ $ preferredViewsOrder ,
243
259
$ otherViews [$ groupLabel ]->choices
244
260
);
245
261
}
0 commit comments