Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4c04601

Browse filesBrowse files
antonch1989fabpot
authored andcommitted
[Form] group_by as callback returns array
1 parent d5d1b50 commit 4c04601
Copy full SHA for 4c04601

File tree

2 files changed

+65
-23
lines changed
Filter options

2 files changed

+65
-23
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php
+27-23Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
6363
$index = 0;
6464
}
6565

66-
// If $groupBy is a callable, choices are added to the group with the
67-
// name returned by the callable. If the callable returns null, the
68-
// choice is not added to any group
66+
// If $groupBy is a callable returning a string
67+
// choices are added to the group with the name returned by the callable.
68+
// If $groupBy is a callable returning an array
69+
// choices are added to the groups with names returned by the callable
70+
// If the callable returns null, the choice is not added to any group
6971
if (\is_callable($groupBy)) {
7072
foreach ($choices as $value => $choice) {
7173
self::addChoiceViewGroupedBy(
@@ -200,9 +202,9 @@ private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $key
200202

201203
private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
202204
{
203-
$groupLabel = $groupBy($choice, $keys[$value], $value);
205+
$groupLabels = $groupBy($choice, $keys[$value], $value);
204206

205-
if (null === $groupLabel) {
207+
if (null === $groupLabels) {
206208
// If the callable returns null, don't group the choice
207209
self::addChoiceView(
208210
$choice,
@@ -219,25 +221,27 @@ private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label
219221
return;
220222
}
221223

222-
$groupLabel = (string) $groupLabel;
224+
$groupLabels = \is_array($groupLabels) ? \array_map('strval', $groupLabels) : [(string) $groupLabels];
223225

224-
// Initialize the group views if necessary. Unnecessarily built group
225-
// views will be cleaned up at the end of createView()
226-
if (!isset($preferredViews[$groupLabel])) {
227-
$preferredViews[$groupLabel] = new ChoiceGroupView($groupLabel);
228-
$otherViews[$groupLabel] = new ChoiceGroupView($groupLabel);
229-
}
226+
foreach ($groupLabels as $groupLabel) {
227+
// Initialize the group views if necessary. Unnecessarily built group
228+
// views will be cleaned up at the end of createView()
229+
if (!isset($preferredViews[$groupLabel])) {
230+
$preferredViews[$groupLabel] = new ChoiceGroupView($groupLabel);
231+
$otherViews[$groupLabel] = new ChoiceGroupView($groupLabel);
232+
}
230233

231-
self::addChoiceView(
232-
$choice,
233-
$value,
234-
$label,
235-
$keys,
236-
$index,
237-
$attr,
238-
$isPreferred,
239-
$preferredViews[$groupLabel]->choices,
240-
$otherViews[$groupLabel]->choices
241-
);
234+
self::addChoiceView(
235+
$choice,
236+
$value,
237+
$label,
238+
$keys,
239+
$index,
240+
$attr,
241+
$isPreferred,
242+
$preferredViews[$groupLabel]->choices,
243+
$otherViews[$groupLabel]->choices
244+
);
245+
}
242246
}
243247
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public function getGroup($object)
7777
return $this->obj1 === $object || $this->obj2 === $object ? 'Group 1' : 'Group 2';
7878
}
7979

80+
public function getGroupArray($object)
81+
{
82+
return $this->obj1 === $object || $this->obj2 === $object ? ['Group 1', 'Group 2'] : ['Group 3'];
83+
}
84+
8085
public function getGroupAsObject($object)
8186
{
8287
return $this->obj1 === $object || $this->obj2 === $object
@@ -462,6 +467,19 @@ public function testCreateViewFlatGroupByAsCallable()
462467
$this->assertGroupedView($view);
463468
}
464469

470+
public function testCreateViewFlatGroupByAsCallableReturnsArray()
471+
{
472+
$view = $this->factory->createView(
473+
$this->list,
474+
[],
475+
null, // label
476+
null, // index
477+
[$this, 'getGroupArray']
478+
);
479+
480+
$this->assertGroupedViewWithChoiceDuplication($view);
481+
}
482+
465483
public function testCreateViewFlatGroupByObjectThatCanBeCastToString()
466484
{
467485
$view = $this->factory->createView(
@@ -773,6 +791,26 @@ private function assertGroupedView($view)
773791
]
774792
), $view);
775793
}
794+
795+
private function assertGroupedViewWithChoiceDuplication($view)
796+
{
797+
$this->assertEquals(new ChoiceListView(
798+
[
799+
'Group 1' => new ChoiceGroupView(
800+
'Group 1',
801+
[0 => new ChoiceView($this->obj1, '0', 'A'), 2 => new ChoiceView($this->obj2, '1', 'B')]
802+
),
803+
'Group 2' => new ChoiceGroupView(
804+
'Group 2',
805+
[1 => new ChoiceView($this->obj1, '0', 'A'), 3 => new ChoiceView($this->obj2, '1', 'B')]
806+
),
807+
'Group 3' => new ChoiceGroupView(
808+
'Group 3',
809+
[4 => new ChoiceView($this->obj3, '2', 'C'), 5 => new ChoiceView($this->obj4, '3', 'D')]
810+
),
811+
], []
812+
), $view);
813+
}
776814
}
777815

778816
class DefaultChoiceListFactoryTest_Castable

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.