Skip to content

Commit b2e03eb

Browse files
committed
feature #478 [Autocomplete] implement max_results option (DarrHeLL)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Autocomplete] implement max_results option | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets |N/A | License | MIT Added max_results option in formType options to control the results number returned by a query on automatic endpoint. Commits ------- 289463d [Autocomplete] implement max_results option
2 parents 7c1d0da + 289463d commit b2e03eb

File tree

7 files changed

+32
-1
lines changed

7 files changed

+32
-1
lines changed

src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public function finishView(FormView $view, FormInterface $form, array $options)
6767
$values['tom-select-options'] = json_encode($options['tom_select_options']);
6868
}
6969

70+
if ($options['max_results']) {
71+
$values['max-results'] = $options['max_results'];
72+
}
73+
7074
$values['no-results-found-text'] = $this->trans($options['no_results_found_text']);
7175
$values['no-more-results-text'] = $this->trans($options['no_more_results_text']);
7276

@@ -87,6 +91,7 @@ public function configureOptions(OptionsResolver $resolver)
8791
'allow_options_create' => false,
8892
'no_results_found_text' => 'No results found',
8993
'no_more_results_text' => 'No more results',
94+
'max_results' => 10,
9095
]);
9196

9297
// if autocomplete_url is passed, then HTML options are already supported

src/Autocomplete/src/Form/AutocompleteEntityTypeSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function preSetData(FormEvent $event)
3939
// pass to AutocompleteChoiceTypeExtension
4040
$options['autocomplete'] = true;
4141
$options['autocomplete_url'] = $this->autocompleteUrl;
42-
unset($options['searchable_fields'], $options['security'], $options['filter_query']);
42+
unset($options['searchable_fields'], $options['security'], $options['filter_query'], $options['max_results']);
4343

4444
$form->add('autocomplete', EntityType::class, $options);
4545
}

src/Autocomplete/src/Form/ParentEntityAutocompleteType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ public function configureOptions(OptionsResolver $resolver)
7676
// set to the string role that's required to view the autocomplete results
7777
// or a callable: function(Symfony\Component\Security\Core\Security $security): bool
7878
'security' => false,
79+
// set the max results number that a query on automatic endpoint return.
80+
'max_results' => 10,
7981
]);
8082

8183
$resolver->setRequired(['class']);
8284
$resolver->setAllowedTypes('security', ['boolean', 'string', 'callable']);
85+
$resolver->setAllowedTypes('max_results', ['int', 'null']);
8386
$resolver->setAllowedTypes('filter_query', ['callable', 'null']);
8487
$resolver->setNormalizer('searchable_fields', function (Options $options, ?array $searchableFields) {
8588
if (null !== $searchableFields && null !== $options['filter_query']) {

src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public function createFilteredQueryBuilder(EntityRepository $repository, string
5454
return $queryBuilder;
5555
}
5656

57+
// Applying max result limit or not
58+
$queryBuilder->setMaxResults($this->getMaxResults());
59+
5760
$this->entitySearchUtil->addSearchClause(
5861
$queryBuilder,
5962
$query,
@@ -131,6 +134,11 @@ private function getFilterQuery(): ?callable
131134
return $this->getForm()->getConfig()->getOption('filter_query');
132135
}
133136

137+
private function getMaxResults(): ?int
138+
{
139+
return $this->getForm()->getConfig()->getOption('max_results');
140+
}
141+
134142
private function getEntityMetadata(): EntityMetadata
135143
{
136144
if (null === $this->entityMetadata) {

src/Autocomplete/src/Resources/doc/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ to the options above, you can also pass:
260260
$qb->andWhere('entity.name LIKE :filter OR entity.description LIKE :filter')
261261
->setParameter('filter', '%'.$query.'%');
262262
}
263+
``max_results`` (default: 10)
264+
Allow you to control the max number of results returned by the automatic autocomplete endpoint.
263265

264266
Using with a TextType Field
265267
---------------------------

src/Autocomplete/tests/Fixtures/Form/CategoryAutocompleteType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function configureOptions(OptionsResolver $resolver)
4141
'attr' => [
4242
'data-controller' => 'custom-autocomplete',
4343
],
44+
'max_results' => 5,
4445
]);
4546
}
4647

src/Autocomplete/tests/Functional/FieldAutocompleterTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,16 @@ public function testItEnforcesSecurity(): void
7878
->assertJsonMatches('length(results)', 3)
7979
;
8080
}
81+
82+
public function testItCheckMaxResultsOption() : void
83+
{
84+
CategoryFactory::createMany(30, ['name' => 'foo']);
85+
86+
$this->browser()
87+
->throwExceptions()
88+
->get('/test/autocomplete/category_autocomplete_type?query=foo')
89+
->assertSuccessful()
90+
->assertJsonMatches('length(results)', 5)
91+
;
92+
}
8193
}

0 commit comments

Comments
 (0)