8000 [Form] Allow choices with duplicated content by ewgRa · Pull Request #16436 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Allow choices with duplicated content #16436

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

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
renaming refactoring
  • Loading branch information
ewgRa committed Nov 3, 2015
commit b364d9c55acaff43d275957ff50e193c0526c198
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ protected function flatten(array $choices, $value, &$choicesByValues, &$rawChoic

$choiceValue = (string) call_user_func($value, $choice);
$choicesByValues[$choiceValue] = $choice;
$rawChoices[$key] = $choice;
$keysByValues[$choiceValue] = $key;
$rawChoices[$key] = $choice;
$rawKeys[$key] = $choiceValue;
$rawLabels[$key] = $key;
$structuredValues[$key] = $choiceValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ protected function flatten(array $choices, $value, &$choicesByValues, &$rawChoic

$choiceValue = (string) call_user_func($value, $choice);
$choicesByValues[$choiceValue] = $choice;
$rawChoices[$choice] = $choiceValue;
$keysByValues[$choiceValue] = $key;
$rawChoices[$choice] = $choiceValue;
$rawKeys[$choice] = $choiceValue;
$rawLabels[$choice] = $key;
$structuredValues[$key] = $choiceValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,45 +149,45 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
return new ChoiceListView($otherViews, $preferredViews);
}

private static function addChoiceView($data, $value, $label, $key, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
private static function addChoiceView($data, $value, $labelCallback, $label, &$index, $attr, $isPreferred, &$preferredViews, &$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.
$nextIndex = is_int($index) ? $index++ : call_user_func($index, $data, $key, $value);
$nextIndex = is_int($index) ? $index++ : call_user_func($index, $data, $label, $value);

$view = new ChoiceView(
$data,
$value,
// If the labels are null, use the original choice key by default
null === $label ? (string) $key : (string) call_user_func($label, $data, $key, $value),
null === $labelCallback ? (string) $label : (string) call_user_func($labelCallback, $data, $label, $value),
// The attributes may be a callable or a mapping from choice indices
// to nested arrays
is_callable($attr) ? call_user_func($attr, $data, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())
is_callable($attr) ? call_user_func($attr, $data, $label, $value) : (isset($attr[$label]) ? $attr[$label] : array())
);

// $isPreferred may be null if no choices are preferred
if ($isPreferred && call_user_func($isPreferred, $data, $key, $value)) {
if ($isPreferred && call_user_func($isPreferred, $data, $label, $value)) {
$preferredViews[$nextIndex] = $view;
} else {
$otherViews[$nextIndex] = $view;
}
A4CF }

private static function addStructuredChoiceViews($structuredValues, $label, $choices, $keys, $labels, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
private static function addStructuredChoiceViews($structuredValues, $labelCallback, $choices, $keys, $labels, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
{
foreach ($structuredValues as $key => $value) {
if (null === $value) {
foreach ($structuredValues as $key => $data) {
if (null === $data) {
continue;
}

// Add the contents of groups to new ChoiceGroupView instances
if (is_array($value)) {
if (is_array($data)) {
$preferredViewsForGroup = array();
$otherViewsForGroup = array();

self::addStructuredChoiceViews(
$value,
$label,
$data,
$labelCallback,
$choices,
$keys[$key],
$labels[$key],
Expand All @@ -211,9 +211,9 @@ private static function addStructuredChoiceViews($structuredValues, $label, $cho

// Add ungrouped items directly
self::addChoiceView(
$value,
$data,
$keys[$key],
$label,
$labelCallback,
$labels[$key],
$index,
$attr,
Expand Down
0