Skip to content

Commit 7eaff09

Browse files
committed
Refactor EntityMetadata and EntityConfigurator to remove targetEntityDisplayField and simplify display logic
1 parent 80c0cb4 commit 7eaff09

2 files changed

Lines changed: 8 additions & 24 deletions

File tree

src/Field/Configurator/EntityConfigurator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
8282
Type\class_string(DashboardControllerInterface::class)->coerce($context->getDashboardControllerFqcn()),
8383
$targetCrudControllerFqcn,
8484
$targetEntityFqcn,
85-
EntityField::getEntityDisplayField($field),
8685
$field->getValue(),
8786
);
8887

@@ -98,10 +97,10 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
9897
throw new RuntimeException('Unknown association type.');
9998
}
10099

101-
if ($entityMetadata->targetEntityDisplayField() !== null) {
102-
// even if the entity has __toString method EntityType form type will not use it if option value is set to NULL.
103-
$field->setFormTypeOption('choice_label', $entityMetadata->targetEntityDisplayField());
104-
}
100+
$field->setFormTypeOption(
101+
'choice_label',
102+
fn (object $targetEntityInstance): string|null => EntityField::formatAsString($targetEntityInstance, $field, $this->translator),
103+
);
105104

106105
$autocompleteMode = Type\bool()->coerce($field->getCustomOption(EntityField::OPTION_AUTOCOMPLETE));
107106
$widgetMode = Type\string()->coerce($field->getCustomOption(EntityField::OPTION_WIDGET));

src/Field/Configurator/EntityConfigurator/EntityMetadata.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
88
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
9-
use Symfony\Contracts\Translation\TranslatableInterface;
109

1110
/**
1211
* @psalm-immutable
@@ -27,33 +26,27 @@ final class EntityMetadata
2726
/** @var class-string */
2827
private string $targetEntityFqcn;
2928

30-
/** @var string|(callable(object):string|TranslatableInterface|null)|null */
31-
private $targetEntityDisplayField;
32-
3329
private object|int|string|null $targetEntityId;
3430

3531
/**
36-
* @param class-string<CrudControllerInterface> $sourceCrudControllerFqcn
37-
* @param class-string<DashboardControllerInterface> $targetDashboardControllerFqcn
38-
* @param class-string<CrudControllerInterface> $targetCrudControllerFqcn
39-
* @param class-string $targetEntityFqcn
40-
* @param string|(callable(object):string|TranslatableInterface|null)|null $targetEntityDisplayField
32+
* @param class-string<CrudControllerInterface> $sourceCrudControllerFqcn
33+
* @param class-string<DashboardControllerInterface> $targetDashboardControllerFqcn
34+
* @param class-string<CrudControllerInterface> $targetCrudControllerFqcn
35+
* @param class-string $targetEntityFqcn
4136
*/
4237
public function __construct(
4338
object|int|string|null $sourceEntityId,
4439
string $sourceCrudControllerFqcn,
4540
string $targetDashboardControllerFqcn,
4641
string $targetCrudControllerFqcn,
4742
string $targetEntityFqcn,
48-
string|callable|null $targetEntityDisplayField,
4943
object|int|string|null $targetEntityId,
5044
) {
5145
$this->sourceEntityId = $sourceEntityId;
5246
$this->sourceCrudControllerFqcn = $sourceCrudControllerFqcn;
5347
$this->targetDashboardControllerFqcn = $targetDashboardControllerFqcn;
5448
$this->targetCrudControllerFqcn = $targetCrudControllerFqcn;
5549
$this->targetEntityFqcn = $targetEntityFqcn;
56-
$this->targetEntityDisplayField = $targetEntityDisplayField;
5750
$this->targetEntityId = $targetEntityId;
5851
}
5952

@@ -94,14 +87,6 @@ public function targetEntityFqcn(): string
9487
return $this->targetEntityFqcn;
9588
}
9689

97-
/**
98-
* @return string|(callable(object):string|TranslatableInterface|null)|null
99-
*/
100-
public function targetEntityDisplayField(): string|callable|null
101-
{
102-
return $this->targetEntityDisplayField;
103-
}
104-
10590
public function targetEntityId(): object|int|string|null
10691
{
10792
return $this->targetEntityId;

0 commit comments

Comments
 (0)