-
-
Notifications
You must be signed in to change notification settings - Fork 394
[Autocomplete]: implement group_by option on Entity Autocompleter results #481
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 |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ final class AutocompleteResults | |
| public function __construct( | ||
| public array $results, | ||
| public bool $hasNextPage, | ||
|
Member
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. We can't break BC here, even though it's unlikely to cause problems. So: A) Rename Then, let's move the |
||
| public array $optgroups = [], | ||
| ) { | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,11 @@ | |
| namespace Symfony\UX\Autocomplete; | ||
|
|
||
| use Doctrine\ORM\Tools\Pagination\Paginator; | ||
| use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException; | ||
| use Symfony\Component\PropertyAccess\PropertyAccessor; | ||
| use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
| use Symfony\Component\PropertyAccess\PropertyPath; | ||
| use Symfony\Component\PropertyAccess\PropertyPathInterface; | ||
| use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
| use Symfony\Component\Security\Core\Security; | ||
| use Symfony\UX\Autocomplete\Doctrine\DoctrineRegistryWrapper; | ||
|
|
@@ -21,10 +26,22 @@ | |
| */ | ||
| final class AutocompleteResultsExecutor | ||
| { | ||
| private PropertyAccessorInterface $propertyAccessor; | ||
| private ?Security $security; | ||
|
|
||
| public function __construct( | ||
| private DoctrineRegistryWrapper $managerRegistry, | ||
| private ?Security $security = null | ||
| $propertyAccessor, | ||
| /* Security $security = null */ | ||
| ) { | ||
| if ($propertyAccessor instanceof Security) { | ||
| trigger_deprecation('symfony/ux-autocomplete', '2.8.0', 'Passing a "%s" instance as the second argument of "%s()" is deprecated, pass a "%s" instance instead.', Security::class, __METHOD__, PropertyAccessorInterface::class); | ||
| $this->security = $propertyAccessor; | ||
| $this->propertyAccessor = new PropertyAccessor(); | ||
| } else { | ||
| $this->propertyAccessor = $propertyAccessor; | ||
| $this->security = \func_num_args() >= 3 ? func_get_arg(2) : null; | ||
| } | ||
| } | ||
|
|
||
| public function fetchResults(EntityAutocompleterInterface $autocompleter, string $query, int $page): AutocompleteResults | ||
|
|
@@ -50,15 +67,61 @@ public function fetchResults(EntityAutocompleterInterface $autocompleter, string | |
| $paginator = new Paginator($queryBuilder); | ||
|
|
||
| $nbPages = (int) ceil($paginator->count() / $queryBuilder->getMaxResults()); | ||
| $hasNextPage = $page < $nbPages; | ||
|
|
||
| $results = []; | ||
|
|
||
| if (null === $groupBy = $autocompleter->getGroupBy()) { | ||
| foreach ($paginator as $entity) { | ||
| $results[] = [ | ||
| 'value' => $autocompleter->getValue($entity), | ||
| 'text' => $autocompleter->getLabel($entity), | ||
| ]; | ||
| } | ||
|
|
||
| return new AutocompleteResults($results, $hasNextPage); | ||
| } | ||
|
|
||
| if (\is_string($groupBy)) { | ||
| $groupBy = new PropertyPath($groupBy); | ||
| } | ||
|
|
||
| if ($groupBy instanceof PropertyPathInterface) { | ||
| $accessor = $this->propertyAccessor; | ||
| $groupBy = function ($choice) use ($accessor, $groupBy) { | ||
| try { | ||
| return $accessor->getValue($choice, $groupBy); | ||
| } catch (UnexpectedTypeException) { | ||
| return null; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| if (!\is_callable($groupBy)) { | ||
| throw new \InvalidArgumentException(sprintf('Option "group_by" must be callable, "%s" given.', get_debug_type($groupBy))); | ||
| } | ||
|
|
||
| $optgroupLabels = []; | ||
|
|
||
| foreach ($paginator as $entity) { | ||
| $results[] = [ | ||
| $result = [ | ||
| 'value' => $autocompleter->getValue($entity), | ||
| 'text' => $autocompleter->getLabel($entity), | ||
| ]; | ||
|
Member
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. Minor, but I think we should "exit early" first if there is no group by. Basically: if (null === $groupBy = $autocompleter->getGroupBy()) {
// use the old logic
$results = [];
foreach ($paginator as $entity) {
$results[] = [
'value' => $autocompleter->getValue($entity),
'text' => $autocompleter->getLabel($entity),
];
}
return $results;
}Then the code that handles |
||
|
|
||
| $groupLabels = $groupBy($entity, $result['value'], $result['text']); | ||
|
|
||
| if (null !== $groupLabels) { | ||
| $groupLabels = \is_array($groupLabels) ? array_map('strval', $groupLabels) : [(string) $groupLabels]; | ||
| $result['group_by'] = $groupLabels; | ||
| $optgroupLabels = array_merge($optgroupLabels, $groupLabels); | ||
| } | ||
|
|
||
| $results[] = $result; | ||
| } | ||
|
|
||
| return new AutocompleteResults($results, $page < $nbPages); | ||
| $optgroups = array_map(fn (string $label) => ['value' => $label, 'label' => $label], array_unique($optgroupLabels)); | ||
|
|
||
| return new AutocompleteResults($results, $hasNextPage, $optgroups); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,4 +46,9 @@ public function getValue(object $entity): mixed; | |
| * Note: if SecurityBundle is not installed, this will not be called. | ||
| */ | ||
| public function isGranted(Security $security): bool; | ||
|
|
||
| /** | ||
| * Return group_by option. | ||
| */ | ||
| public function getGroupBy(): mixed; | ||
|
Member
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. Instead of physically adding this method, which would break BC, add some documentation to the top for it: https://github.com/SymfonyCasts/reset-password-bundle/blob/6601f15fce7a4934bc9570f76feaf1b93536b1a7/src/ResetPasswordHelperInterface.php#L19 - that will allow people to be aware of it, and then we will add the actual method whenever we release a new major version.
Member
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. Also is the return value really mixed? Or is it
Contributor
Author
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. Yes i think, It can be string, callable or null
Member
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. That is QUITE the long list of things :). I think we could probably just use
Contributor
Author
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. I'm struggling to see how to normalize this, how do you see the implementation of this normalization |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter; | ||
|
|
||
| use Doctrine\ORM\EntityRepository; | ||
| use Doctrine\ORM\QueryBuilder; | ||
| use Symfony\Component\HttpFoundation\RequestStack; | ||
| use Symfony\Component\Security\Core\Security; | ||
| use Symfony\UX\Autocomplete\Doctrine\EntitySearchUtil; | ||
| use Symfony\UX\Autocomplete\EntityAutocompleterInterface; | ||
| use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Product; | ||
|
|
||
| class CustomGroupByProductAutocompleter extends CustomProductAutocompleter | ||
| { | ||
| public function getGroupBy(): mixed | ||
| { | ||
| return 'category.name'; | ||
| } | ||
| } |
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 change is not valid anymore, right?
And we need docs for the new option :)