简体   繁体   中英

SonataAdminBundle show field as label

I have enum field status which I want to show in label label-info html. Like it shows for boolean value (red or green).

In there demo they have lots of labels, but I can't find on how to add them.

在此输入图像描述

You have to create a custom template for the field you want to customise the rendering.

For your status field, create a template like this :

// src/AcmeBundle/Resources/views/CRUD/status_field.html.twig

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

{% block field %}
    {% set status = object.status == 1 ? 'success' : 'danger' %}
    <div>
        <span class="label label-{{ status }}">{{ object.status }}</span><br />
    </div>
{% endblock %}

Configure it as template in your admin class :

$listMapper
    // ...
    ->add('status', null, array(
        'template' => 'AcmeBundle:CRUD:status_field.html.twig'
    ))

Explained in the little but helpful part of the documentation .

I actually tested with the sonata admin Bundle version 2.3.3, and it's as easy as this:

/**
 * @param ListMapper $listMapper
 */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('status', 'boolean');
}

in your admin class.

The custom template works but it is not necessary in this case as it already implemented. The vendor template is located in vendor/sonata-project/admin-bundle/Resources/views/CRUD/list_boolean.html.twig .

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