7

I want show the list of multiple attributes Name => value in a table overriding single field of only for PortsAdmin in ShowMapper

Ports Entity mapped with PortsAttributes Entity.

Relation of entity is OneToMany Ports with multiple attributes.

Admin View (Edit Action)

Edit View of attributes listing

Show Action

Show list of attributes

I want change attribute view same as edit Action.

Noman
  • 4,088
  • 1
  • 21
  • 36

2 Answers2

18

You can create a custom template for the PostAttributes:

Example:

/* ShowMapper in admin */
$showMapper->add('attributes', null, array(
    'template' => 'YOUR_TEMPLATE.html.twig' // <-- This is the trick
));

In your template, you can extend the base show field (SonataAdminBundle:CRUD:base_show_field.html.twig or @SonataAdmin/CRUD/base_show_field.html.twig for symfony > 4.0), and override the field block. The variable named value stores the data in twig.

Example:

YOUR_TEMPLATE.html.twig

{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}
{# for sf > 4.0 #}
{#  {% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %} #}

{% block field %}
    {% for val in value %}
        {{ val.name }} - {{ val.value }} {# I'm just guessing the object properties #}
        <br/>
    {% endfor %}
{% endblock %}
Fracsi
  • 2,274
  • 15
  • 24
  • I have tried the same trick and it didn't work ! I'm using symfony 3.4 and this `template` attribute works only with symfony 3.1 is there another way to override the `show_field` view ? – SlimenTN Jan 19 '20 at 09:21
0

@SlimenTN you can try changing this line in template file:

{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}

with this:

{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}

The rest of code seems ok (I have the same in a SF4 project)

RL83
  • 39
  • 8