Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autocomplete context crud controller for embedded forms #6788

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from

Conversation

zorn-v
Copy link
Contributor

@zorn-v zorn-v commented Feb 5, 2025

If you have embedded form with association autocomplete field and have defined setQueryBuilder there, then it will not applied as crudControllerFqcn comes from request now.
But request has info about parent controller, not embedded. So configurator can not find field with configured query builder and it does not applied.

@zorn-v
Copy link
Contributor Author

zorn-v commented Feb 5, 2025

As workaround for now

# config/services.yaml
services:
#...
    App\EasyAdmin\Field\Configurator\AssociationConfigurator:
        tags:
            - { name: 'ea.field_configurator', priority: -1 }
<?php

namespace App\EasyAdmin\Field\Configurator;

use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;

/** fix EA autocomplete bug in embedded forms */
final class AssociationConfigurator implements FieldConfiguratorInterface
{
    public function supports(FieldDto $field, EntityDto $entityDto): bool
    {
        return AssociationField::class === $field->getFieldFqcn();
    }

    public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $context): void
    {
        if (true === $field->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE)) {
            $endpointUrl = $field->getFormTypeOption('attr.data-ea-autocomplete-endpoint-url');
            [$path, $query] = explode('?', $endpointUrl);
            parse_str($query, $data);
            $data['autocompleteContext'][EA::CRUD_CONTROLLER_FQCN] = $context->getCrudControllers()->findCrudFqcnByEntityFqcn($entityDto->getFqcn());
            $endpointUrl = $path.'?'.http_build_query($data);
            $field->setFormTypeOption('attr.data-ea-autocomplete-endpoint-url', $endpointUrl);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant