@@ -52,8 +52,12 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
52
52
return new LazyChoiceList ($ loader , $ value );
53
53
}
54
54
55
- public function createView (ChoiceListInterface $ list , array |callable $ preferredChoices = null , callable |false $ label = null , callable $ index = null , callable $ groupBy = null , array |callable $ attr = null , array |callable $ labelTranslationParameters = []): ChoiceListView
55
+ /**
56
+ * @param bool $duplicatePreferredChoices
57
+ */
58
+ public function createView (ChoiceListInterface $ list , array |callable $ preferredChoices = null , callable |false $ label = null , callable $ index = null , callable $ groupBy = null , array |callable $ attr = null , array |callable $ labelTranslationParameters = []/* , bool $duplicatePreferredChoices = true */ ): ChoiceListView
56
59
{
60
+ $ duplicatePreferredChoices = \func_num_args () > 7 ? func_get_arg (7 ) : true ;
57
61
$ preferredViews = [];
58
62
$ preferredViewsOrder = [];
59
63
$ otherViews = [];
@@ -92,7 +96,8 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
92
96
$ preferredChoices ,
93
97
$ preferredViews ,
94
98
$ preferredViewsOrder ,
95
- $ otherViews
99
+ $ otherViews ,
100
+ $ duplicatePreferredChoices ,
96
101
);
97
102
}
98
103
@@ -130,7 +135,8 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
130
135
$ preferredChoices ,
131
136
$ preferredViews ,
132
137
$ preferredViewsOrder ,
133
- $ otherViews
138
+ $ otherViews ,
139
+ $ duplicatePreferredChoices ,
134
140
);
135
141
}
136
142
@@ -139,7 +145,7 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
139
145
return new ChoiceListView ($ otherViews , $ preferredViews );
140
146
}
141
147
142
- private static function addChoiceView ($ choice , string $ value , $ label , array $ keys , &$ index , $ attr , $ labelTranslationParameters , ?callable $ isPreferred , array &$ preferredViews , array &$ preferredViewsOrder , array &$ otherViews ): void
148
+ private static function addChoiceView ($ choice , string $ value , $ label , array $ keys , &$ index , $ attr , $ labelTranslationParameters , ?callable $ isPreferred , array &$ preferredViews , array &$ preferredViewsOrder , array &$ otherViews, bool $ duplicatePreferredChoices ): void
143
149
{
144
150
// $value may be an integer or a string, since it's stored in the array
145
151
// keys. We want to guarantee it's a string though.
@@ -180,12 +186,16 @@ private static function addChoiceView($choice, string $value, $label, array $key
180
186
if (null !== $ isPreferred && false !== $ preferredKey = $ isPreferred ($ choice , $ key , $ value )) {
181
187
$ preferredViews [$ nextIndex ] = $ view ;
182
188
$ preferredViewsOrder [$ nextIndex ] = $ preferredKey ;
183
- }
184
189
185
- $ otherViews [$ nextIndex ] = $ view ;
190
+ if ($ duplicatePreferredChoices ) {
191
+ $ otherViews [$ nextIndex ] = $ view ;
192
+ }
193
+ } else {
194
+ $ otherViews [$ nextIndex ] = $ view ;
195
+ }
186
196
}
187
197
188
- private static function addChoiceViewsFromStructuredValues (array $ values , $ label , array $ choices , array $ keys , &$ index , $ attr , $ labelTranslationParameters , ?callable $ isPreferred , array &$ preferredViews , array &$ preferredViewsOrder , array &$ otherViews ): void
198
+ private static function addChoiceViewsFromStructuredValues (array $ values , $ label , array $ choices , array $ keys , &$ index , $ attr , $ labelTranslationParameters , ?callable $ isPreferred , array &$ preferredViews , array &$ preferredViewsOrder , array &$ otherViews, bool $ duplicatePreferredChoices ): void
189
199
{
190
200
foreach ($ values as $ key => $ value ) {
191
201
if (null === $ value ) {
@@ -208,7 +218,8 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
208
218
$ isPreferred ,
209
219
$ preferredViewsForGroup ,
210
220
$ preferredViewsOrder ,
211
- $ otherViewsForGroup
221
+ $ otherViewsForGroup ,
222
+ $ duplicatePreferredChoices ,
212
223
);
213
224
214
225
if (\count ($ preferredViewsForGroup ) > 0 ) {
@@ -234,12 +245,13 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
234
245
$ isPreferred ,
235
246
$ preferredViews ,
236
247
$ preferredViewsOrder ,
237
- $ otherViews
248
+ $ otherViews ,
249
+ $ duplicatePreferredChoices ,
238
250
);
239
251
}
240
252
}
241
253
242
- private static function addChoiceViewsGroupedByCallable (callable $ groupBy , $ choice , string $ value , $ label , array $ keys , &$ index , $ attr , $ labelTranslationParameters , ?callable $ isPreferred , array &$ preferredViews , array &$ preferredViewsOrder , array &$ otherViews ): void
254
+ private static function addChoiceViewsGroupedByCallable (callable $ groupBy , $ choice , string $ value , $ label , array $ keys , &$ index , $ attr , $ labelTranslationParameters , ?callable $ isPreferred , array &$ preferredViews , array &$ preferredViewsOrder , array &$ otherViews, bool $ duplicatePreferredChoices ): void
243
255
{
244
256
$ groupLabels = $ groupBy ($ choice , $ keys [$ value ], $ value );
245
257
@@ -256,7 +268,8 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
256
268
$ isPreferred ,
257
269
$ preferredViews ,
258
270
$ preferredViewsOrder ,
259
- $ otherViews
271
+ $ otherViews ,
272
+ $ duplicatePreferredChoices ,
260
273
);
261
274
262
275
return ;
@@ -286,7 +299,8 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
286
299
$ isPreferred ,
287
300
$ preferredViews [$ groupLabel ]->choices ,
288
301
$ preferredViewsOrder [$ groupLabel ],
289
- $ otherViews [$ groupLabel ]->choices
302
+ $ otherViews [$ groupLabel ]->choices ,
303
+ $ duplicatePreferredChoices ,
290
304
);
291
305
}
292
306
}
0 commit comments