-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Form] Keep preferred choices order in ChoiceType #30985
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,13 +48,16 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul | |
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null) | ||
{ | ||
$preferredViews = []; | ||
$preferredViewsOrder = []; | ||
$otherViews = []; | ||
$choices = $list->getChoices(); | ||
$keys = $list->getOriginalKeys(); | ||
|
||
if (!\is_callable($preferredChoices) && !empty($preferredChoices)) { | ||
$preferredChoices = function ($choice) use ($preferredChoices) { | ||
return \in_array($choice, $preferredChoices, true); | ||
// make sure we have keys that reflect order | ||
$preferredChoices = array_values($preferredChoices); | ||
$preferredChoices = static function ($choice) use ($preferredChoices) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello, I'm new to reviews in symfony. Should the usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This closure is only used if the user didn't configure the |
||
return array_search($choice, $preferredChoices, true); | ||
}; | ||
} | ||
|
||
|
@@ -80,6 +83,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null, | |
$attr, | ||
$preferredChoices, | ||
$preferredViews, | ||
$preferredViewsOrder, | ||
$otherViews | ||
); | ||
} | ||
|
@@ -108,14 +112,21 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null, | |
$attr, | ||
$preferredChoices, | ||
$preferredViews, | ||
$preferredViewsOrder, | ||
$otherViews | ||
); | ||
} | ||
|
||
uksort($preferredViews, static function ($a, $b) use ($preferredViewsOrder): int { | ||
return isset($preferredViewsOrder[$a], $preferredViewsOrder[$b]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is needed when we have groups in preferred views. |
||
? $preferredViewsOrder[$a] <=> $preferredViewsOrder[$b] | ||
: 0; | ||
}); | ||
|
||
return new ChoiceListView($otherViews, $preferredViews); | ||
} | ||
|
||
private static function addChoiceView($choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews) | ||
private static function addChoiceView($choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews) | ||
{ | ||
// $value may be an integer or a string, since it's stored in the array | ||
// keys. We want to guarantee it's a string though. | ||
|
@@ -143,14 +154,15 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $ | |
); | ||
|
||
// $isPreferred may be null if no choices are preferred | ||
if ($isPreferred && $isPreferred($choice, $key, $value)) { | ||
if ($isPreferred && false !== $preferredKey = $isPreferred($choice, $key, $value)) { | ||
$preferredViews[$nextIndex] = $view; | ||
$preferredViewsOrder[$nextIndex] = $preferredKey; | ||
} else { | ||
$otherViews[$nextIndex] = $view; | ||
} | ||
} | ||
|
||
private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews) | ||
private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews) | ||
{ | ||
foreach ($values as $key => $value) { | ||
if (null === $value) { | ||
|
@@ -171,6 +183,7 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho | |
$attr, | ||
$isPreferred, | ||
$preferredViewsForGroup, | ||
$preferredViewsOrder, | ||
$otherViewsForGroup | ||
); | ||
|
||
|
@@ -195,12 +208,13 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho | |
$attr, | ||
$isPreferred, | ||
$preferredViews, | ||
$preferredViewsOrder, | ||
$otherViews | ||
); | ||
} | ||
} | ||
|
||
private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews) | ||
private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews) | ||
{ | ||
$groupLabels = $groupBy($choice, $keys[$value], $value); | ||
|
||
|
@@ -215,6 +229,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu | |
$attr, | ||
$isPreferred, | ||
$preferredViews, | ||
$preferredViewsOrder, | ||
$otherViews | ||
); | ||
|
||
|
@@ -240,6 +255,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu | |
$attr, | ||
$isPreferred, | ||
$preferredViews[$groupLabel]->choices, | ||
$preferredViewsOrder, | ||
$otherViews[$groupLabel]->choices | ||
); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.