-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Component][Form][ChoiceType] : Add new option to sort group_by value #22136
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
Conversation
@@ -81,6 +81,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null, | |||
$otherViews | |||
); | |||
} | |||
|
|||
if ($groupByOrder && is_array($groupByOrder)) { | |||
uksort($otherViews, function ($key1, $key2) use ($groupByOrder) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this logic does not seem to support callable values for $groupByOrder
, while the interface allows it (and callables can be arrays, making things even worse).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see this support both callables and predefined arrays. Something like this should work:
if ($groupByOrder) {
if (is_callable($groupByOrder)) {
// TODO
} elseif (is_array($groupByOrder)) {
// TODO
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@colinodell , thanks
Can you give me an example of callable groupByOrder you need ?
Which parameters you need in the function ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The callable should be compatible with uksort
- anything that takes two parameters, compares them, and returns an integer. Your code would then look something like this:
if ($groupByOrder) {
if (is_callable($groupByOrder)) {
uksort($otherViews, $groupByOrder);
} elseif (is_array($groupByOrder)) {
// TODO: Your existing code goes here
}
}
I could then use any type of callable for the groupByOrder
option:
'strnatcmp'
- Sort them using a "natural order" algorithm via thestrnatcmp()
function[$this, 'sortGroups']
- Use thesortGroups()
function on$this
[Foo::class, 'sortGroups']
- Use the staticsortGroups()
function from theFoo
classfunction ($a, $b) { return $b <=> $a; }
- Use some anonymous function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@colinodell thanks, will do that soon
* | ||
* @return ChoiceListView The choice list view | ||
*/ | ||
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null); | ||
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $groupByOrder = null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding arguments in an interface is forbidden by our BC policy
@@ -356,6 +357,7 @@ public function configureOptions(OptionsResolver $resolver) | |||
$resolver->setAllowedTypes('choice_attr', array('null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); | |||
$resolver->setAllowedTypes('preferred_choices', array('array', '\Traversable', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); | |||
$resolver->setAllowedTypes('group_by', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); | |||
$resolver->setAllowedTypes('group_by_order', array('null', 'array', '\Traversable')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does not match the types supported by the implementation.
@@ -148,10 +148,11 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul | |||
* @param null|callable|string|PropertyPath $index The callable or path generating the view indices | |||
* @param null|callable|string|PropertyPath $groupBy The callable or path generating the group names | |||
* @param null|array|callable|string|PropertyPath $attr The callable or path generating the HTML attributes | |||
* @param null|array|callable|string|PropertyPath $groupByOrder The groupBy order |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does not support a PropertyPath, as you don't handle it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does PropertyPath
even make sense here?
@@ -152,7 +152,7 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null) | |||
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $groupByOrder = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new value must be taken into account when building the cache key, otherwise a list could be reused while it was having a different order
@stof thanks for your review. If adding a parameter is a BC break, do you have an advice to deal a new parameter ? or do it differently ? |
@stof a little ping on my comments to continue the PR if you want ^^ Also, same remark as @colinodell , How do you see the callable groupByOrder ? Which parameters we propose in the callable function ? |
ping @stof |
Moving to 4.1. Rebase on master might be needed, where PHP 7.1 features can be used btw. |
I'm 👎 on this feature. The problem solved here can easily be solved by sorting the choices before passing them to the |
A Proposal PR to have the possibility to define an order for group_by value without create a difficult twig rendering or override buildView Form