Skip to content
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

[AutocompleteBundle] Custom autocompleter never found by ServiceLocator #2598

Open
SimonRethore opened this issue Feb 24, 2025 · 0 comments
Open
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed

Comments

@SimonRethore
Copy link

SimonRethore commented Feb 24, 2025

I have created a custom autocompleter following this : https://symfony.com/bundles/ux-autocomplete/current/index.html#advanced-creating-an-autocompleter-with-no-form.

But this autocompleter is not used by EntityAutocompleteController and AutocompleterRegistry which always return Symfony\UX\Autocomplete\Form\WrappedEntityTypeAutocompleter.

symfony/ux-autocomplete: v2.23.0

DogAutocompleteType:


namespace App\Form\Autocomplete;

use App\Entity\Dog;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;

#[AsEntityAutocompleteField]
class DogAutocompleteType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'class' => Dog::class,
            'choice_label' => fn (Dog $dog) => $dog->getId().' - '.$dog->getName(),
            'searchable_fields' => ['id', 'name'],
        ]);
    }

    public function getParent(): string
    {
        return BaseEntityAutocompleteType::class;
    }
}

DogAutocompleter:


namespace App\Autocompleter;

use App\Entity\Dog;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\UX\Autocomplete\OptionsAwareEntityAutocompleterInterface;

 #[AutoconfigureTag('ux.entity_autocompleter', ['alias' => 'dog_autocomplete_type', 'priority' => 100])]
class DogAutocompleter implements OptionsAwareEntityAutocompleterInterface
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    ) {
    }

    /**
     * @var array<string, mixed>
     */
    private array $options = [];

    public function createFilteredQueryBuilder(EntityRepository $repository, string $query): \Doctrine\ORM\QueryBuilder
    {
        ...

        return $qb;
    }

    public function getEntityClass(): string
    {
        return Dog::class;
    }

    public function getLabel(object $entity): string
    {
        return $entity->getId().' - '.$entity->getName();
    }

    public function getValue(object $entity): mixed
    {
        return $entity;
    }

    public function isGranted(Security $security): bool
    {
        return true;
    }

    public function setOptions(array $options): void
    {
        $this->options = $options;
    }
}

As you can see my autocompleter should be in priority :
Image

@SimonRethore SimonRethore added the Bug Bug Fix label Feb 24, 2025
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed
Projects
None yet
Development

No branches or pull requests

2 participants