Skip to content

[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

Merged
merged 1 commit into from
May 3, 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.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I'm new to reviews in symfony. Should the usage of static be considered a BC break? It's unlikely someone relies on a Factory in preferredChoices, but possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This closure is only used if the user didn't configure the preferred_choices option as a callable (but as an array). And since we do not access the surrounding object context this change is fine.

return array_search($choice, $preferredChoices, true);
};
}

Expand All @@ -80,6 +83,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
$attr,
$preferredChoices,
$preferredViews,
$preferredViewsOrder,
$otherViews
);
}
Expand Down Expand Up @@ -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])
Copy link
Contributor Author

@vudaltsov vudaltsov Apr 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is needed when we have groups in preferred views.
It happens in tests and might happen in user land.

? $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.
Expand Down Expand Up @@ -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) {
Expand All @@ -171,6 +183,7 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho
$attr,
$isPreferred,
$preferredViewsForGroup,
$preferredViewsOrder,
$otherViewsForGroup
);

Expand All @@ -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);

Expand All @@ -215,6 +229,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
$attr,
$isPreferred,
$preferredViews,
$preferredViewsOrder,
$otherViews
);

Expand All @@ -240,6 +255,7 @@ private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $valu
$attr,
$isPreferred,
$preferredViews[$groupLabel]->choices,
$preferredViewsOrder,
$otherViews[$groupLabel]->choices
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,28 @@ public function testCreateViewFlatPreferredChoices()
$this->assertFlatView($view);
}

public function testCreateViewFlatPreferredChoicesSameOrder()
{
$view = $this->factory->createView(
$this->list,
[$this->obj2, $this->obj1, $this->obj4, $this->obj3]
);

$preferredLabels = array_map(static function (ChoiceView $view): string {
return $view->label;
}, $view->preferredChoices);

$this->assertSame(
[
1 => 'B',
0 => 'A',
3 => 'D',
2 => 'C',
],
$preferredLabels
);
}

public function testCreateViewFlatPreferredChoicesEmptyArray()
{
$view = $this->factory->createView(
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy