diff --git a/src/Field/Configurator/CommonPreConfigurator.php b/src/Field/Configurator/CommonPreConfigurator.php index 250a397a73..5f6ad4b108 100644 --- a/src/Field/Configurator/CommonPreConfigurator.php +++ b/src/Field/Configurator/CommonPreConfigurator.php @@ -8,6 +8,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface; use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; +use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory; use EasyCorp\Bundle\EasyAdminBundle\Field\AvatarField; use EasyCorp\Bundle\EasyAdminBundle\Field\FormField; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; @@ -21,10 +22,12 @@ final class CommonPreConfigurator implements FieldConfiguratorInterface { private PropertyAccessorInterface $propertyAccessor; + private EntityFactory $entityFactory; - public function __construct(PropertyAccessorInterface $propertyAccessor) + public function __construct(PropertyAccessorInterface $propertyAccessor, EntityFactory $entityFactory) { $this->propertyAccessor = $propertyAccessor; + $this->entityFactory = $entityFactory; } public function supports(FieldDto $field, EntityDto $entityDto): bool @@ -153,6 +156,26 @@ private function buildSortableOption(FieldDto $field, EntityDto $entityDto): boo return $isSortable; } + // if it's an association, check recursively if the nested property exists + if ($entityDto->isAssociation($field->getProperty())) { + $associatedProperties = explode('.', $field->getProperty()); + for ($i = 0; $i < \count($associatedProperties); $i++) { + $property = $associatedProperties[$i]; + if (!$entityDto->hasProperty($property)) { + return false; + } + + // try to get the entity associated to the property, except for the last property + if (isset($associatedProperties[$i + 1])) { + $propertyMetadata = $entityDto->getPropertyMetadata($property); + $targetEntity = $propertyMetadata->get('targetEntity'); + $entityDto = $this->entityFactory->create($targetEntity); + } + } + + return true; + } + return $entityDto->hasProperty($field->getProperty()); } diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index 578b124711..21da8afbcd 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -304,6 +304,7 @@ ->set(CommonPreConfigurator::class) ->arg(0, new Reference('property_accessor')) + ->arg(1, service(EntityFactory::class)) ->tag(EasyAdminExtension::TAG_FIELD_CONFIGURATOR, ['priority' => 9999]) ->set(CountryConfigurator::class)