Search code examples
symfonysonata-adminsymfony-sonata

Symfony 2 | Sonata : collection remove numbers


I'm searching for days now. I have a collection in my admin entity, everything works and I get : Collection in form

I want to remove this numbers (0 and 1 here).

My NewsAdmin

$formMapper
        ->add('comments', 'collection', array('type' => new ...\NewsCommentType(),
                                                    'allow_add'    => false,
                                                    'allow_delete' => false,
                                                    'by_reference' => false,
                                                    "required" => false,
                                                    "label" => "Comments"))
    ;

And my NewsCommentType :

class NewsCommentType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('content', null, array(
            "disabled" => true,
            "label" => false)
        )
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => '...\NewsComment'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'newscomment';
}
}

And my edit_news.html.twig :

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

{% block javascripts %}
    {{ parent() }}
    <script src="{{ asset("bundles/.../js/ckeditor/ckeditor.js") }}"></script>
    <script type="text/javascript">

    </script>
{% endblock %}

{% block stylesheets %}
    {{ parent() }}
{% endblock %}


{% block formactions %}
<div class="well well-small form-actions">
    <button type="submit" class="btn btn-success" name="btn_update_and_edit"><i class="fa fa-save"></i> {{ 'btn_update_and_edit_again'|trans({}, 'SonataAdminBundle') }}</button>
    <button type="submit" class="btn btn-success" name="btn_update_and_list"><i class="fa fa-save"></i> <i class="fa fa-list"></i> {{ 'btn_update_and_return_to_list'|trans({}, 'SonataAdminBundle') }}</button>
    <a class="btn btn-success" href="{{ admin.generateUrl('list') }}">
    <i class="fa fa-list"></i>
    {{ 'link_action_list'|trans({}, 'SonataAdminBundle') }}</a>
</div>
{% endblock formactions %}

There is nothing in Sonata doc about it (or i missed it) so I don't know where to start. Is there an attribute with collection ? I have to do it in javascript ?


Solution

  • This numbers are the result of using sonata and type collection. You have to use sonata_type_collection with your custom type and set 'edit' and 'inline' values (only available with sonata_type_collection) to custom your result :

    In your admin class :

    $formMapper
            ->add('comments', 'sonata_type_collection', array('type' => new ...\NewsCommentType(), 
                'by_reference' => true,
                'required' => false),     
                array(
                    'edit' => 'inline',
                    'inline' => 'table',
                    'sortable' => 'id')
                );
        ;
    

    And in your AbstractType, you can display the field you want to :

     $builder
        ->add('content', null, array(
            "disabled" => true,
            "label" => false)
        )
    ;
    

    The result is not perfect but there is no more numbers ;)

    enter image description here