@@ -52,8 +52,12 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
5252 return new LazyChoiceList ($ loader , $ value );
5353 }
5454
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
5659 {
60+ $ duplicatePreferredChoices = \func_num_args () > 7 ? func_get_arg (7 ) : true ;
5761 $ preferredViews = [];
5862 $ preferredViewsOrder = [];
5963 $ otherViews = [];
@@ -92,7 +96,8 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
9296 $ preferredChoices ,
9397 $ preferredViews ,
9498 $ preferredViewsOrder ,
95- $ otherViews
99+ $ otherViews ,
100+ $ duplicatePreferredChoices ,
96101 );
97102 }
98103
@@ -130,7 +135,8 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
130135 $ preferredChoices ,
131136 $ preferredViews ,
132137 $ preferredViewsOrder ,
133- $ otherViews
138+ $ otherViews ,
139+ $ duplicatePreferredChoices ,
134140 );
135141 }
136142
@@ -139,7 +145,7 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
139145 return new ChoiceListView ($ otherViews , $ preferredViews );
140146 }
141147
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
143149 {
144150 // $value may be an integer or a string, since it's stored in the array
145151 // keys. We want to guarantee it's a string though.
@@ -180,12 +186,16 @@ private static function addChoiceView($choice, string $value, $label, array $key
180186 if (null !== $ isPreferred && false !== $ preferredKey = $ isPreferred ($ choice , $ key , $ value )) {
181187 $ preferredViews [$ nextIndex ] = $ view ;
182188 $ preferredViewsOrder [$ nextIndex ] = $ preferredKey ;
183- }
184189
185- $ otherViews [$ nextIndex ] = $ view ;
190+ if ($ duplicatePreferredChoices ) {
191+ $ otherViews [$ nextIndex ] = $ view ;
192+ }
193+ } else {
194+ $ otherViews [$ nextIndex ] = $ view ;
195+ }
186196 }
187197
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
189199 {
190200 foreach ($ values as $ key => $ value ) {
191201 if (null === $ value ) {
@@ -208,7 +218,8 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
208218 $ isPreferred ,
209219 $ preferredViewsForGroup ,
210220 $ preferredViewsOrder ,
211- $ otherViewsForGroup
221+ $ otherViewsForGroup ,
222+ $ duplicatePreferredChoices ,
212223 );
213224
214225 if (\count ($ preferredViewsForGroup ) > 0 ) {
@@ -234,12 +245,13 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
234245 $ isPreferred ,
235246 $ preferredViews ,
236247 $ preferredViewsOrder ,
237- $ otherViews
248+ $ otherViews ,
249+ $ duplicatePreferredChoices ,
238250 );
239251 }
240252 }
241253
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
243255 {
244256 $ groupLabels = $ groupBy ($ choice , $ keys [$ value ], $ value );
245257
@@ -256,7 +268,8 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
256268 $ isPreferred ,
257269 $ preferredViews ,
258270 $ preferredViewsOrder ,
259- $ otherViews
271+ $ otherViews ,
272+ $ duplicatePreferredChoices ,
260273 );
261274
262275 return ;
@@ -286,7 +299,8 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
286299 $ isPreferred ,
287300 $ preferredViews [$ groupLabel ]->choices ,
288301 $ preferredViewsOrder [$ groupLabel ],
289- $ otherViews [$ groupLabel ]->choices
302+ $ otherViews [$ groupLabel ]->choices ,
303+ $ duplicatePreferredChoices ,
290304 );
291305 }
292306 }
0 commit comments