Add request origin for EntityFilter autocomplete() requests

Open
#7,617 2 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
php, symfony
Domain
backend

Research direction

Compare the EntityFilter autocomplete URL construction with the AssociationField's existing autocomplete context, then trace the request into the target CRUD controller's createIndexQueryBuilder(). Confirm that EntityFilter autocomplete requests carry the needed origin context and that the query-builder use case works without the page crashing.

Written by the indexing model from the issue text.

Description

feature

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;
}
Dominant language
PHP
Stars
4.3k
Forks
1.1k
Avg merge
2d 9h
Merged PRs (30d)
52

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from EasyCorp/EasyAdminBundle

All issues in EasyCorp/EasyAdminBundle

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.