Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Field/Configurator/CommonPreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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());
}

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down