Skip to content

[Autocomplete] implement max_results option #478

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
Sep 27, 2022
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
5 changes: 5 additions & 0 deletions src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function finishView(FormView $view, FormInterface $form, array $options)
$values['tom-select-options'] = json_encode($options['tom_select_options']);
}

if ($options['max_results']) {
$values['max-results'] = $options['max_results'];
}

$values['no-results-found-text'] = $this->trans($options['no_results_found_text']);
$values['no-more-results-text'] = $this->trans($options['no_more_results_text']);

Expand All @@ -87,6 +91,7 @@ public function configureOptions(OptionsResolver $resolver)
'allow_options_create' => false,
'no_results_found_text' => 'No results found',
'no_more_results_text' => 'No more results',
'max_results' => 10,
]);

// if autocomplete_url is passed, then HTML options are already supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function preSetData(FormEvent $event)
// pass to AutocompleteChoiceTypeExtension
$options['autocomplete'] = true;
$options['autocomplete_url'] = $this->autocompleteUrl;
unset($options['searchable_fields'], $options['security'], $options['filter_query']);
unset($options['searchable_fields'], $options['security'], $options['filter_query'], $options['max_results']);

$form->add('autocomplete', EntityType::class, $options);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Autocomplete/src/Form/ParentEntityAutocompleteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ public function configureOptions(OptionsResolver $resolver)
// set to the string role that's required to view the autocomplete results
// or a callable: function(Symfony\Component\Security\Core\Security $security): bool
'security' => false,
// set the max results number that a query on automatic endpoint return.
'max_results' => 10,
]);

$resolver->setRequired(['class']);
$resolver->setAllowedTypes('security', ['boolean', 'string', 'callable']);
$resolver->setAllowedTypes('max_results', ['int', 'null']);
$resolver->setAllowedTypes('filter_query', ['callable', 'null']);
$resolver->setNormalizer('searchable_fields', function (Options $options, ?array $searchableFields) {
if (null !== $searchableFields && null !== $options['filter_query']) {
Expand Down
8 changes: 8 additions & 0 deletions src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function createFilteredQueryBuilder(EntityRepository $repository, string
return $queryBuilder;
}

// Applying max result limit or not
$queryBuilder->setMaxResults($this->getMaxResults());

$this->entitySearchUtil->addSearchClause(
$queryBuilder,
$query,
Expand Down Expand Up @@ -131,6 +134,11 @@ private function getFilterQuery(): ?callable
return $this->getForm()->getConfig()->getOption('filter_query');
}

private function getMaxResults(): ?int
{
return $this->getForm()->getConfig()->getOption('max_results');
}

private function getEntityMetadata(): EntityMetadata
{
if (null === $this->entityMetadata) {
Expand Down
2 changes: 2 additions & 0 deletions src/Autocomplete/src/Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ to the options above, you can also pass:
$qb->andWhere('entity.name LIKE :filter OR entity.description LIKE :filter')
->setParameter('filter', '%'.$query.'%');
}
``max_results`` (default: 10)
Allow you to control the max number of results returned by the automatic autocomplete endpoint.

Using with a TextType Field
---------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function configureOptions(OptionsResolver $resolver)
'attr' => [
'data-controller' => 'custom-autocomplete',
],
'max_results' => 5,
]);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Autocomplete/tests/Functional/FieldAutocompleterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,16 @@ public function testItEnforcesSecurity(): void
->assertJsonMatches('length(results)', 3)
;
}

public function testItCheckMaxResultsOption() : void
{
CategoryFactory::createMany(30, ['name' => 'foo']);

$this->browser()
->throwExceptions()
->get('/test/autocomplete/category_autocomplete_type?query=foo')
->assertSuccessful()
->assertJsonMatches('length(results)', 5)
;
}
}