-
-
Notifications
You must be signed in to change notification settings - Fork 364
[Autocomplete] New autocomplete type #1158
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# CHANGELOG | ||
|
||
## 2.13.0 | ||
|
||
- Add new BaseEntityAutocompleteType | ||
|
||
## 2.9.0 | ||
|
||
- Add support for symfony/asset-mapper | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/Autocomplete/src/Form/BaseEntityAutocompleteType.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Autocomplete\Form; | ||
|
||
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Exception\RuntimeException; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\UX\Autocomplete\Form\ChoiceList\Loader\ExtraLazyChoiceLoader; | ||
|
||
/** | ||
* All form types that want to expose autocomplete functionality should use this for its getParent(). | ||
*/ | ||
final class BaseEntityAutocompleteType extends AbstractType | ||
{ | ||
public function __construct( | ||
private UrlGeneratorInterface $urlGenerator, | ||
) { | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder->setAttribute('autocomplete_url', $this->getAutocompleteUrl($builder, $options)); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$choiceLoader = static function (Options $options, $loader) { | ||
return new ExtraLazyChoiceLoader($loader); | ||
}; | ||
|
||
$resolver->setDefaults([ | ||
'autocomplete' => true, | ||
'choice_loader' => $choiceLoader, | ||
// set to the fields to search on or null to search on all fields | ||
'searchable_fields' => null, | ||
// override the search logic - set to a callable: | ||
// function(QueryBuilder $qb, string $query, EntityRepository $repository) { | ||
// $qb->andWhere('entity.name LIKE :filter OR entity.description LIKE :filter') | ||
// ->setParameter('filter', '%'.$query.'%'); | ||
// } | ||
'filter_query' => null, | ||
// 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->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']) { | ||
throw new RuntimeException('Both the searchable_fields and filter_query options cannot be set.'); | ||
} | ||
|
||
return $searchableFields; | ||
}); | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return EntityType::class; | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return 'ux_entity_autocomplete'; | ||
} | ||
|
||
/** | ||
* Uses the provided URL, or auto-generate from the provided alias. | ||
*/ | ||
private function getAutocompleteUrl(FormBuilderInterface $builder, array $options): string | ||
{ | ||
if ($options['autocomplete_url']) { | ||
return $options['autocomplete_url']; | ||
} | ||
|
||
$formType = $builder->getType()->getInnerType(); | ||
$attribute = AsEntityAutocompleteField::getInstance($formType::class); | ||
|
||
if (!$attribute) { | ||
throw new \LogicException(sprintf('You must either provide your own autocomplete_url, or add #[AsEntityAutocompleteField] attribute to %s.', $formType::class)); | ||
} | ||
|
||
return $this->urlGenerator->generate($attribute->getRoute(), [ | ||
'alias' => $attribute->getAlias() ?: AsEntityAutocompleteField::shortName($formType::class), | ||
]); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/Autocomplete/src/Form/ChoiceList/Loader/ExtraLazyChoiceLoader.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Autocomplete\Form\ChoiceList\Loader; | ||
|
||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; | ||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
|
||
/** | ||
* Loads choices on demand only. | ||
*/ | ||
class ExtraLazyChoiceLoader implements ChoiceLoaderInterface | ||
{ | ||
private ?ChoiceListInterface $choiceList = null; | ||
private array $choices = []; | ||
private bool $cached = false; | ||
|
||
public function __construct( | ||
private readonly ChoiceLoaderInterface $decorated, | ||
) { | ||
} | ||
|
||
public function loadChoiceList(callable $value = null): ChoiceListInterface | ||
{ | ||
if (null !== $this->choiceList && $this->cached) { | ||
return $this->choiceList; | ||
} | ||
|
||
$this->cached = true; | ||
|
||
return $this->choiceList = new ArrayChoiceList($this->choices, $value); | ||
} | ||
|
||
public function loadChoicesForValues(array $values, callable $value = null): array | ||
{ | ||
if ($this->choices !== $choices = $this->decorated->loadChoicesForValues($values, $value)) { | ||
$this->cached = false; | ||
} | ||
|
||
return $this->choices = $choices; | ||
} | ||
|
||
public function loadValuesForChoices(array $choices, callable $value = null): array | ||
{ | ||
$values = $this->decorated->loadValuesForChoices($choices, $value); | ||
|
||
if ([] === $values || [''] === $values) { | ||
$newChoices = []; | ||
} else { | ||
$newChoices = $choices; | ||
} | ||
|
||
if ($this->choices !== $newChoices) { | ||
$this->choices = $newChoices; | ||
$this->cached = false; | ||
} | ||
|
||
return $values; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 9 additions & 3 deletions
12
src/Autocomplete/templates/autocomplete_form_theme.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
{# EasyAdminAutocomplete form type #} | ||
{% block ux_entity_autocomplete_widget %} | ||
{{ form_widget(form.autocomplete, { attr: form.autocomplete.vars.attr|merge({ required: required }) }) }} | ||
{% if form.autocomplete is defined %} | ||
{{ form_widget(form.autocomplete, { attr: form.autocomplete.vars.attr|merge({ required: required }) }) }} | ||
{% else %} | ||
{{ form_widget(form) }} | ||
{% endif %} | ||
{% endblock ux_entity_autocomplete_widget %} | ||
|
||
{% block ux_entity_autocomplete_label %} | ||
{% set id = form.autocomplete.vars.id %} | ||
{% if form.autocomplete is defined %} | ||
{% set id = form.autocomplete.vars.id %} | ||
{% endif %} | ||
{{ block('form_label') }} | ||
{% endblock ux_entity_autocomplete_label %} | ||
{% endblock ux_entity_autocomplete_label %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we deprecate this service? Or does that trigger a deprecation SIMPLY because it's a valid form type (even if it's unused)?
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.
deprecated