Home > Software engineering >  Render as multiple bagdes with an AssociationField in EasyAdmin
Render as multiple bagdes with an AssociationField in EasyAdmin

Time:05-25

I got an issue with that question.

I've make this to render a "string" for a ManyToMany relation :

->formatValue(function ($value, $entity) {
return implode(",",$entity->getCategories()->toArray());
})

and it works pretty good ! But I've a question !

How can I render many badges in Index ? Because this method render one unique badge with "Value 1, Value 2"... And I want to see 2 badges, one with "Value 1" and one other with "Value 2" in the same line. Someone know how to do that ?

I hope my question is clear. Noé

CodePudding user response:

You need to create a custom template that does it.

Use easy admin ->setTemplatePath() method to override your field template.

Example:

->setTemplatePath('fields/yourEntity/categories.html.twig')

And your twig template loop through each values to render it with multiple badges:

{% for value in field.value %}
    <span >
        {{ value }}
    </span>
{% else %}
    <span >
        None
    </span>
{% endfor %}

You should get a badge for each categories, you could also customize how to render those badge (with different colors ?) by using {{ value }} and any of its method to render it differently.

  • Related