Using autocompletes
The SyliusBootstrapAdminUi package uses Symfony UX under the hood. Thus, UX autocomplete is already setup and configured in your admin panel. This means that any simple ChoiceType filter form can be turned into an autocomplete simply by setting autocomplete to true in form options.
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'choices' => $this->getChoices(),
'placeholder' => 'sylius.ui.all',
'autocomplete' => true,
]);
}
public function getParent(): string
{
return ChoiceType::class;
}
However, if your autocomplete filter requires fetching data from another entity, you will need to use a BaseEntityAutocompleteType in order to fetch your options via AJAX.
All you need to start leveraging this functionality is a bit of routing config.
Configure the entity autocomplete route
<?php
declare(strict_types=1);
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
return static function (RoutingConfigurator $routingConfigurator): void {
// ...
$routingConfigurator
->add('ux_entity_autocomplete_admin', '/admin/autocomplete/{alias}')
->controller('ux.autocomplete.entity_autocomplete_controller')
;
};
# ...
ux_entity_autocomplete_admin:
path: '/admin/autocomplete/{alias}'
controller: 'ux.autocomplete.entity_autocomplete_controller'This adds a new ux_entity_autocomplete_admin AJAX route dedicated to your autocompletes.
Add a grid filter with entity autocomplete
First, you need to create an entity autocomplete field .
Then, you need to create your grid filter.
1) Create your filter class.
2) then configure the Twig template this filter will use.
PHP config :
YAML config:
Now that the filter is configured, you can use it inside any grid.
Last updated
Was this helpful?