1

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é

1 Answers1

1

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 class="badge badge-info">
        {{ value }}
    </span>
{% else %}
    <span class="badge badge-secondary">
        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.

Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
  • You can technically do some tricky stuff using formatValue, but the default association template add your value inside a badge, so you would need to do some hacky stuff to close the current badge and add a new one for each values. Basically it would be an ugly way to do it. So it's better to have a clean template (or create a generic one) that manage to do it. – Dylan KAS May 23 '22 at 15:56