EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.06k stars 1.02k forks source link

CollectionField entry label generation #6358

Open blablabla1234678 opened 3 months ago

blablabla1234678 commented 3 months ago

I have a CRUD controller like the following:

class PricingPlanCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return PricingPlan::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('name'),
            IntegerField::new('price'),
            CollectionField::new('benefits')
                ->setEntryType(PricingPlanBenefitType::class)
                ->onlyOnForms()
        ];
    }
}

The benefits are shown this way on the list currently:

App\Entity\PricingPlanBenefit #1

So this is the default __toString output of PHP.

If I want to change the labelling, then I have to override the PricingPlanBenefit.__toString e.g.

    public function __toString():string
    {
        return $this->name;
    }

The problem with this that we are writing View logic into Model, so it violates separation of concerns.

It would be nice to have something like:

            CollectionField::new('benefits')
                ->setEntryType(PricingPlanBenefitType::class)
                ->setEntryLabel(function (PricingPlanBenefit $entity){return $entity->getName();})
                ->onlyOnForms()

As far as I can tell this feature is not supported by EasyAdmin currently. Maybe I am wrong, I am new to this project.

webbird commented 2 months ago

I think this could also solve my question in #6360