Skip to content

Add request origin for EntityFilter autocomplete() requests #7617

@siggidiel

Description

@siggidiel

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions