Short description of what this feature will allow to do:
Using the EntityFilter with autocomplete() adds additional context to the AJAX requests, so related controllers can check for this context inside the createIndexQueryBuilder() method. Similar, what the AssociationField is already doing.
Right now, if you want to limit the list of selectable products, you need to add the value_type_options.query_builder callable to the EntityFilter form type options. But this only works for as long as you're not using the autocomplete() feature. As soon as you add autocomplete, the page crashes, because the option query_builder does not exist.
Example of how to use this feature
I added a custom filter + custom filter configurator which is basically a copy of the \EasyCorp\Bundle\EasyAdminBundle\Filter\Configurator\EntityConfigurator. I added the following context:
$autocompleteEndpointUrl = $this->adminUrlGenerator
->unsetAll()
->set('page', 1)
->setController($targetCrudControllerFqcn)
->setAction('autocomplete')
->set(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT, [
'propertyName' => $propertyName,
'originatingPage' => $context->getCrud()?->getCurrentAction() ?? throw new \LogicException('The current action is not defined.'),
+ 'filterSource' => 'whateverProperty', // Needs to be set from filterDto automatically maybe?
+ 'originatingCrudControllerFqcn' => $context->getRequest()->attributes->get(EA::CRUD_CONTROLLER_FQCN),
])
->generateUrl();
And inside the createIndexQueryBuilder() method of my related crud controller, I am doing:
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
$queryBuilder = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
$autocompleteContext = $this->getContext()?->getRequest()->query->all(AssociationField::PARAM_AUTOCOMPLETE_CONTEXT);
if (
($autocompleteContext['filterSource'] ?? null) === 'whateverProperty'
&& ($autocompleteContext['originatingCrudControllerFqcn'] ?? null) === MyCrudController::class
) {
$queryBuilder->innerJoin('entity.relatedEntity', 'relatedEntity');
}
return $queryBuilder;
}
Short description of what this feature will allow to do:
Using the
EntityFilterwithautocomplete()adds additional context to the AJAX requests, so related controllers can check for this context inside thecreateIndexQueryBuilder()method. Similar, what theAssociationFieldis already doing.Right now, if you want to limit the list of selectable products, you need to add the
value_type_options.query_buildercallable to theEntityFilterform type options. But this only works for as long as you're not using theautocomplete()feature. As soon as you add autocomplete, the page crashes, because the optionquery_builderdoes not exist.Example of how to use this feature
I added a custom filter + custom filter configurator which is basically a copy of the
\EasyCorp\Bundle\EasyAdminBundle\Filter\Configurator\EntityConfigurator. I added the following context:And inside the
createIndexQueryBuilder()method of my related crud controller, I am doing: