-
-
Save wizhippo/597bf56cd69dfd3682d8a0eee67ac5c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Form\Filter\Type; | |
use EasyCorp\Bundle\EasyAdminBundle\Form\EventListener\CrudAutocompleteSubscriber; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\DataMapperInterface; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\Form\FormView; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class AutocompleteEntityFilterType extends AbstractType implements DataMapperInterface | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->addEventSubscriber(new CrudAutocompleteSubscriber()) | |
->setDataMapper($this) | |
; | |
} | |
public function finishView(FormView $view, FormInterface $form, array $options) | |
{ | |
// Add a custom block prefix to inner field to ease theming: | |
array_splice($view['autocomplete']->vars['block_prefixes'], -1, 0, 'ea_autocomplete_inner'); | |
// allowClear option needs a placeholder value (it can be empty) | |
if (($view->vars['attr']['data-allow-clear'] ?? false) && (!isset($view->vars['attr']['data-placeholder']))) { | |
$view->vars['attr']['data-placeholder'] = ''; | |
} | |
} | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults([ | |
'multiple' => false, | |
// force display errors on this form field | |
'error_bubbling' => false, | |
'placeholder' => '', | |
]); | |
$resolver->setRequired(['class']); | |
} | |
public function getBlockPrefix(): string | |
{ | |
return 'ea_autocomplete'; | |
} | |
public function mapDataToForms($viewData, $forms) | |
{ | |
$form = current(iterator_to_array($forms, false)); | |
$form->setData($viewData); | |
} | |
public function mapFormsToData($forms, &$viewData) | |
{ | |
$form = current(iterator_to_array($forms, false)); | |
$viewData = $form->getData(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\EasyAdmin\Filter\Configurator; | |
use App\Form\Filter\Type\AutocompleteEntityFilterType; | |
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA; | |
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; | |
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterConfiguratorInterface; | |
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; | |
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; | |
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDto; | |
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField; | |
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter; | |
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator; | |
final class EntityFilterConfigurator implements FilterConfiguratorInterface | |
{ | |
private AdminUrlGenerator $adminUrlGenerator; | |
public function __construct(AdminUrlGenerator $adminUrlGenerator) | |
{ | |
$this->adminUrlGenerator = $adminUrlGenerator; | |
} | |
public function supports( | |
FilterDto $filterDto, | |
?FieldDto $fieldDto, | |
EntityDto $entityDto, | |
AdminContext $context | |
): bool { | |
return EntityFilter::class === $filterDto->getFqcn(); | |
} | |
public function configure( | |
FilterDto $filterDto, | |
?FieldDto $fieldDto, | |
EntityDto $entityDto, | |
AdminContext $context | |
): void { | |
$propertyName = $filterDto->getProperty(); | |
if (!$entityDto->isAssociation($propertyName)) { | |
return; | |
} | |
if ($fieldDto && $fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE)) { | |
$doctrineMetadata = $entityDto->getPropertyMetadata($propertyName); | |
$filterDto->setFormTypeOptionIfNotSet('value_type_options.attr.data-ea-widget', 'ea-autocomplete'); | |
$targetEntityFqcn = $doctrineMetadata->get('targetEntity'); | |
$targetCrudControllerFqcn = $context->getCrudControllers()->findCrudFqcnByEntityFqcn($targetEntityFqcn); | |
if ($targetCrudControllerFqcn) { | |
$filterDto->setFormTypeOptionIfNotSet('value_type', AutocompleteEntityFilterType::class); | |
$autocompleteEndpointUrl = $this->adminUrlGenerator | |
->set('page', 1) | |
->setController($targetCrudControllerFqcn) | |
->setAction('autocomplete') | |
->setEntityId(null) | |
->unset(EA::SORT) | |
->set('autocompleteContext', [ | |
EA::CRUD_CONTROLLER_FQCN => $context->getRequest()->query->get(EA::CRUD_CONTROLLER_FQCN), | |
'propertyName' => $propertyName, | |
'originatingPage' => $context->getCrud()->getCurrentAction(), | |
]) | |
->generateUrl() | |
; | |
$filterDto->setFormTypeOption( | |
'value_type_options.attr.data-ea-autocomplete-endpoint-url', | |
$autocompleteEndpointUrl | |
); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
_defaults: | |
autowire: true | |
autoconfigure: true | |
App\EasyAdmin\Filter\Configurator\: | |
resource: '../../src/EasyAdmin/Filter/Configurator/*' | |
exclude: '../../src/EasyAdmin/Filter/Configurator/Abstract*' | |
App\EasyAdmin\Filter\Configurator\AutocompleteEntityFilterConfigurator: | |
tags: | |
- { name: ea.filter_configurator } | |
App\EasyAdmin\Filter\Configurator\EntityFilterConfigurator: | |
tags: | |
- { name: ea.filter_configurator } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment