diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php index 15b1661d90..c4681fe20b 100644 --- a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php +++ b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php @@ -70,7 +70,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface // If the callable returns null, the choice is not added to any group if (\is_callable($groupBy)) { foreach ($choices as $value => $choice) { - self::addChoiceViewGroupedBy( + self::addChoiceViewsGroupedByCallable( $groupBy, $choice, (string) $value, @@ -83,9 +83,23 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface $otherViews ); } + + // Remove empty group views that may have been created by + // addChoiceViewsGroupedByCallable() + foreach ($preferredViews as $key => $view) { + if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) { + unset($preferredViews[$key]); + } + } + + foreach ($otherViews as $key => $view) { + if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) { + unset($otherViews[$key]); + } + } } else { // Otherwise use the original structure of the choices - self::addChoiceViewsGroupedBy( + self::addChoiceViewsFromStructuredValues( $list->getStructuredValues(), $label, $choices, @@ -98,20 +112,6 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface ); } - // Remove any empty group view that may have been created by - // addChoiceViewGroupedBy() - foreach ($preferredViews as $key => $view) { - if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) { - unset($preferredViews[$key]); - } - } - - foreach ($otherViews as $key => $view) { - if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) { - unset($otherViews[$key]); - } - } - return new ChoiceListView($otherViews, $preferredViews); } @@ -150,9 +150,9 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface } } - private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews) + private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews) { - foreach ($groupBy as $key => $value) { + foreach ($values as $key => $value) { if (null === $value) { continue; } @@ -162,7 +162,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface $preferredViewsForGroup = []; $otherViewsForGroup = []; - self::addChoiceViewsGroupedBy( + self::addChoiceViewsFromStructuredValues( $value, $label, $choices, @@ -200,7 +200,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface } } - private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews) + private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews) { $groupLabels = $groupBy($choice, $keys[$value], $value);