简体   繁体   中英

How to override Show field in sonata admin

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)

编辑属性列表视图

Show Action

显示属性列表

I want change attribute view same as edit Action.

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 %}

@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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM