8000 [Form] group_by as callback returns array by antonch1989 · Pull Request #30429 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] group_by as callback returns array #30429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
8000 Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
$index = 0;
}

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

private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
{
$groupLabel = $groupBy($choice, $keys[$value], $value);
$groupLabels = $groupBy($choice, $keys[$value], $value);

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

$groupLabel = (string) $groupLabel;
$groupLabels = \is_array($groupLabels) ? \array_map('strval', $groupLabels) : [(string) $groupLabels];

// Initialize the group views if necessary. Unnecessarily built group
// views will be cleaned up at the end of createView()
if (!isset($preferredViews[$groupLabel])) {
$preferredViews[$groupLabel] = new ChoiceGroupView($groupLabel);
$otherViews[$groupLabel] = new ChoiceGroupView($groupLabel);
}
foreach ($groupLabels as $groupLabel) {
// Initialize the group views if necessary. Unnecessarily built group
// views will be cleaned up at the end of createView()
if (!isset($preferredViews[$groupLabel])) {
$preferredViews[$groupLabel] = new ChoiceGroupView($groupLabel);
$otherViews[$groupLabel] = new ChoiceGroupView($groupLabel);
}

self::addChoiceView(
$choice,
$value,
$label,
$keys,
$index,
$attr,
$isPreferred,
$preferredViews[$groupLabel]->choices,
$otherViews[$groupLabel]->choices
);
self::addChoiceView(
$choice,
$value,
$label,
$keys,
$index,
$attr,
$isPreferred,
$preferredViews[$groupLabel]->choices,
$otherViews[$groupLabel]->choices
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public function getGroup($object)
return $this->obj1 === $object || $this->obj2 === $object ? 'Group 1' : 'Group 2';
}

public function getGroupArray($object)
{
return $this->obj1 === $object || $this->obj2 === $object ? ['Group 1', 'Group 2'] : ['Group 3'];
}

public function getGroupAsObject($object)
{
return $this->obj1 === $object || $this->obj2 === $object
Expand Down Expand Up @@ -462,6 +467,19 @@ public function testCreateViewFlatGroupByAsCallable()
$this->assertGroupedView($view);
}

public function testCreateViewFlatGroupByAsCallableReturnsArray()
{
$view = $this->factory->createView(
$this->list,
[],
null, // label
null, // index
[$this, 'getGroupArray']
);

$this->assertGroupedViewWithChoiceDuplication($view);
}

public function testCreateViewFlatGroupByObjectThatCanBeCastToString()
{
$view = $this->factory->createView(
Expand Down Expand Up @@ -773,6 +791,26 @@ private function assertGroupedView($view)
]
), $view);
}

private function assertGroupedViewWithChoiceDuplication($view)
{
$this->assertEquals(new ChoiceListView(
[
'Group 1' => new ChoiceGroupView(
'Group 1',
[0 => new ChoiceView($this->obj1, '0', 'A'), 2 => new ChoiceView($this->obj2, '1', 'B')]
),
'Group 2' => new ChoiceGroupView(
'Group 2',
[1 => new ChoiceView($this->obj1, '0', 'A'), 3 => new ChoiceView($this->obj2, '1', 'B')]
),
'Group 3' => new ChoiceGroupView(
'Group 3',
[4 => new ChoiceView($this->obj3, '2', 'C'), 5 => new ChoiceView($this->obj4, '3', 'D')]
),
], []
), $view);
}
}

class DefaultChoiceListFactoryTest_Castable
Expand Down
0