Search code examples
phpsymfonysonata-admin

Sonata Admin conflict between 'template' and 'editable'


I am currently working on a small personal project to learn how to manipulate Symfony and Sonata, and I find myself confronted with a small problem. I have constrained one of my variables to a "template" in "configureListFields" but I can not submit it to "editable". I can do the one without the other but not both at the same time if not the "editable" bug as I show you a bit further down.

List :

            $listMapper->add('status', 'string', array(
            'template' => 'WebBundle:Default:list_client.html.twig',
            'label'=> 'Status'))

Form :

            $formMapper->add('Status', 'choice', array(
            'choices' => array(
                'Client' => 'Client',
                'Ex-Client' => 'Ex-Client',
                'Prospect' => 'Prospect')))

Template :

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

{% block field %}
<div>
    <p class="ClientStatus {% if object.Status == 'Ex-Client' %}
        label label-danger
       {% elseif object.Status == 'Client'   %}
       label label-success
       {%  else %}
       label label-info

    {%  endif %}" >
        {{ object.Status }}
    </p>
</div>
{% endblock %}

View with that config :

view

Second config :

            $listMapper->add('status', 'choice', array(
            'choices'=>array(
                "Client"=>"Client",
                "New Client"=>"New Client",
                "Ex-Client"=>"Ex Client"
            ),
            'label'=> 'Status',
            'editable'=>true))

View :

view

Third Config :

            ->add('status', 'choice', array(
            'choices'=>array(
                "Client"=>"Client",
                "New Client"=>"New Client",
                "Ex-Client"=>"Ex Client"
            ),
            'template' => 'WebBundle:Default:list_client.html.twig',
            'label'=> 'Status',
            'editable'=>true))

view :

view

So there seems to be a conflict between "template" and "editable" opinions on how to handle this problem ? Thanks a lot.


Solution

  • check out the list_choice template that is provided with the Sonata sources.

    # SonataAdminBundle/Resources/views/CRUD/list_choice.html.twig
    {% set is_editable =
        field_description.options.editable is defined and
        field_description.options.editable and
        admin.hasAccess('edit', object)
    %}
    
    {% set x_editable_type = field_description.type|sonata_xeditable_type %}
    
    {% if is_editable and x_editable_type %}
        {% block field_span_attributes %}
            {% spaceless %}
                {{ parent() }}
                data-source="{{ field_description|sonata_xeditable_choices|json_encode }}"
            {% endspaceless %}
        {% endblock %}
    {% endif %}
    

    They check if the field is editable and add data-source attribute to the field_span_attributes block of the parent base_list_field template part:

    <span {% block field_span_attributes %}class="x-editable"
        data-type="{{ xEditableType }}"
        data-value="{{ data_value }}"
        data-title="{{ field_description.label|trans({}, field_description.translationDomain) }}"
        data-pk="{{ admin.id(object) }}"
        data-url="{{ url }}" {% endblock %}>
        {{ block('field') }}
    </span>
    

    So try adding the data-source in your custom template as well.