Building on top of the most upvoted answer, you could make the Twig snippet universal like this:
{% for member in field.value %}
{%- if field.customOption('crudControllerFqcn') is not empty -%}
{%- set url = ea_url()
.setController(field.customOption('crudControllerFqcn'))
.setAction('detail')
.setEntityId(member.id)
-%}
<a href="{{ url }}">
{{ member }}
</a>
{%- else -%}
{{ member }}
{%- endif -%}
{%- if not loop.last %}, {% endif -%}
{% else %}
<span class="badge badge-secondary">None</span>
{% endfor %}
This way you could set the link destination in your CRUD controller and use the template everywhere. When no CRUD controller is not set, you will still have the individual items enumerated, but not as links.
In my TeamCrudController, I'm doing this:
public function configureFields(string $pageName): \Generator
{
yield TextField::new('name')
->setDisabled()
;
$members = AssociationField::new('members')
->hideOnForm()
;
if (Crud::PAGE_DETAIL === $pageName) { // I want to see the number on INDEX, but the list on DETAIL
$members
->setCrudController(EmployeeCrudController::class)
->setTemplatePath('admin/field/expanded_association_field.html.twig')
;
}
yield $members;
}
Alternatively, if you would like to become the default behaviour, override the EasyAdminBundle template by placing the following content in templates/bundles/EasyAdminBundle/crud/field/association.html.twig
:
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
{% if 'toMany' == field.customOptions.get('associationType') %}
{% if ea.crud.currentPage == 'detail' %}
{% for member in field.value %}
{%- if field.customOption('crudControllerFqcn') is not empty -%}
{%- set url = ea_url()
.setController(field.customOption('crudControllerFqcn'))
.setAction('detail')
.setEntityId(member.id)
-%}
<a href="{{ url }}">
{{- member -}}
</a>
{%- else -%}
{{ member }}
{%- endif -%}
{%- if not loop.last %}, {% endif -%}
{% else %}
<span class="badge badge-secondary">None</span>
{% endfor %}
{% else %}
<span class="badge badge-secondary">{{ field.formattedValue }}</span>
{% endif %}
{% else %}
{% if field.customOptions.get('relatedUrl') is not null %}
<a href="{{ field.customOptions.get('relatedUrl') }}">{{ field.formattedValue }}</a>
{% else %}
{{ field.formattedValue }}
{% endif %}
{% endif %}
formatValue
func, thanks. For the purpose of your solution maybe you can useimplode
(php.net/manual/en/function.implode.php) to return the same result? – Almira