Skip to content

Commit

Permalink
Improve performance of the configurator applied to all fields
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Jul 23, 2022
1 parent 579fcb4 commit 0c9ba07
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/Field/Configurator/CommonPreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\AvatarField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use function Symfony\Component\String\u;
use function Symfony\Component\Translation\t;
Expand Down Expand Up @@ -39,8 +40,14 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c

// if a field already has set a value, someone has written something to
// it (as a virtual field or overwrite); don't modify the value in that case
if (null === $field->getValue()) {
$value = $this->buildValueOption($field, $entityDto);
$isReadable = true;
if (null === $value = $field->getValue()) {
try {
$value = null === $entityDto->getInstance() ? null : $this->propertyAccessor->getValue($entityDto->getInstance(), $field->getProperty());
} catch (AccessException) {

This comment has been minimized.

Copy link
@woodo01

woodo01 Oct 19, 2022

I guess it is not enough only to catch AccessException, because PropertyAccessor throws UnexpectedTypeException too.
The UnexpectedTypeException exception is used in isWritable method of the PropertyAccessor here.

Now there is an error PropertyAccessor requires a graph of objects or arrays to operate on, but it found type "NULL when it tries to get value from a property of a nullable object by relation.
E.g. if properyName is entity.value and entity is null.

$isReadable = false;
}

$field->setValue($value);
$field->setFormattedValue($value);
}
Expand All @@ -57,7 +64,7 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
$isVirtual = $this->buildVirtualOption($field, $entityDto);
$field->setVirtual($isVirtual);

$templatePath = $this->buildTemplatePathOption($context, $field, $entityDto);
$templatePath = $this->buildTemplatePathOption($context, $field, $entityDto, $isReadable);
$field->setTemplatePath($templatePath);

$doctrineMetadata = $entityDto->hasProperty($field->getProperty()) ? $entityDto->getPropertyMetadata($field->getProperty())->all() : [];
Expand All @@ -80,18 +87,6 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
$field->setFormTypeOptionIfNotSet('label', $field->getLabel());
}

private function buildValueOption(FieldDto $field, EntityDto $entityDto)
{
$entityInstance = $entityDto->getInstance();
$propertyName = $field->getProperty();

if (null === $entityInstance || !$this->propertyAccessor->isReadable($entityInstance, $propertyName)) {
return null;
}

return $this->propertyAccessor->getValue($entityInstance, $propertyName);
}

private function buildHelpOption(FieldDto $field, string $translationDomain): ?TranslatableInterface
{
$help = $field->getHelp();
Expand Down Expand Up @@ -161,15 +156,14 @@ private function buildVirtualOption(FieldDto $field, EntityDto $entityDto): bool
return !$entityDto->hasProperty($field->getProperty());
}

private function buildTemplatePathOption(AdminContext $adminContext, FieldDto $field, EntityDto $entityDto): string
private function buildTemplatePathOption(AdminContext $adminContext, FieldDto $field, EntityDto $entityDto, bool $isReadable): string
{
if (null !== $templatePath = $field->getTemplatePath()) {
return $templatePath;
}

// if field has a value set, don't display it as inaccessible (needed e.g. for virtual fields)
$isPropertyReadable = null !== $entityDto->getInstance() && $this->propertyAccessor->isReadable($entityDto->getInstance(), $field->getProperty());
if (!$isPropertyReadable && null === $field->getValue()) {
if (!$isReadable && null === $field->getValue()) {
return $adminContext->getTemplatePath('label/inaccessible');
}

Expand Down

0 comments on commit 0c9ba07

Please sign in to comment.