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 custom grid filter.
First, create your custom filter class.
Then, configure the Twig template this filter will use.
If you use PHP config files (which we recommend you do!), here is how you configure a filter template:
Otherwise, if you're using YAML config files, here is the config for the filter template:
Now that the filter is configured, you can use it inside any grid.
Last updated
Was this helpful?