Easyadminbundle: list associated items in detail view

Created on 4 Jul 2020  路  5Comments  路  Source: EasyCorp/EasyAdminBundle

In detail view when I add a field for an association, it displays the number of associated items. How can I display a clickable list of associated items, so that it shows the name of the item as returned by the __toString method and links it to the detail view of that associated item.

I recall making this work in 2.x but I have not figured it out in 3.x and I do not see it in the docs or issues.

Thank you.

Most helpful comment

check ArrayField
a list is displayed, but unfortunately not clickable

All 5 comments

check ArrayField
a list is displayed, but unfortunately not clickable

@colonelclick
Response from @javiereguiluz

(in the future we'll add the possibility to do that ... no estimated date yet)
We did this because otherwise EA3 would have not been released yet
you can define a custom template fragment for that field in the Detail page and loop for the list of values to display their value (and links too?)

@mdeherder Thanks for digging this up for me! I was already thinking about the fragment route for the time being. I am really enjoying the new direction of EA3!

@mdeherder Thanks for digging this up for me! I was already thinking about the fragment route for the time being. I am really enjoying the new direction of EA3!

me too!

A simple approach
For an AssociationField in 'detail' page : List all items in table format, each row is clickable.
Create a custom template in App/templates/Bundles/EasyAdminBundle/crud/field/association.html.twig

{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
{# @var entity \EasyCor{# p\Bundle\EasyAdminBundle\Dto\EntityDto #}
{% if 'toMany' == field.customOptions.get('associationType') %}
    {% if 'detail' == ea.crud.currentPage %}
        {% set maxLi = field.customOptions.get('maxLi')  ?? 4 %}
        {% set toDisplay = field.customOptions.get('toDisplay')  ?? '' %}
        {% for row in field.value %}
            {% if loop.first %}
                <div{% if field.value|length > maxLi %} style="height:{{ (maxLi + 1) * 22.4 }}px; overflow:scroll;"{% endif %}>
                    <table><tbody>
            {% endif %}
                        <tr><td>
                            <a class="pr-3" href="
                                {{- ea_url().unset('query').unset('filters').setController(ea.crudControllers.findCrudFqcnByEntityFqcn(field.formTypeOption('class'))).setAction('detail').setEntityId(row.id).generateUrl() -}}">
                                {% if toDisplay %}{{ attribute(row,toDisplay) }}{% else %}{{ row }}{% endif %}
                            </a>
                        </td></tr>
            {% if loop.last %}
                    </tbody></table>
                </div>
            {% endif %}
        {% 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 %}

The number of lines and the field to be displayed can be set as follows:

yield AssociationField::new('name', 'label')->onlyOnDetail()
            ->setCustomOptions(['maxLi'=>10,'toDisplay'=>'fullTitle'])

Implicitly: 4 lines and the toString result.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cve picture cve  路  4Comments

lukasluecke picture lukasluecke  路  3Comments

Wait4Code picture Wait4Code  路  3Comments

Ealenn picture Ealenn  路  3Comments

bocharsky-bw picture bocharsky-bw  路  3Comments