Search code examples
viewsonata-adminsymfony-sonatasymfony

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.

enter image description here


Solution

  • 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.