Disable 'add' with sonata_type_collection in SonataAdminBundle forms

11,261

Solution 1

Try this

->add('store_orders', 'sonata_type_collection', array(
      'btn_add' => false
    ))

When you add a collection to Sonata admin forms, by default an "Add New" button is displayed, to prevent the "Add New" Button or "+" from displaying, set the add_btn key to FALSE in the array, which is the third parameter in the add function.

Solution 2

I'm not very clued up with the SonataAdminBundle, but two options jump to mind.

First is to use type collection instead of sonata_type_collection. I'm not sure what the results will be with this change, but you can give it a bash and see what happens.

The other option is to override the template with one of your own.

Copy

src\bundles\Sonata\AdminBundle\Resources\views\Form\form_admin_fields.html.twig

to

app\Resources\SonataAdminBundle\views\Form\form_admin_fields.html.twig

and just remove the section

{% if allow_add %}

or you can just call

{% extends "SonataAdminBundle:Form:form_admin_fields.html.twig %}

and just change the {% block collection_widget %}

I'm sure there is a better way of acheiving this, but I'm still a Symfony2 noob and this is the only way I can think off.

Share:
11,261

Related videos on Youtube

user1207727
Author by

user1207727

Updated on June 04, 2022

Comments

  • user1207727
    user1207727 almost 2 years

    Does anyone out there know how I can get rid of the green pluses that allow adding a new item to a collection in the sonata admin forms? The native collectiontype has allow_add & allow_delete, but sonata_type_collection doesn't seem to notice those options.

    I have tried the following:

        ->add('store_orders', 'sonata_type_collection', array(), array(
          'type_options' => array('allow_add' => false),
        ))
    

    which has no effect

        ->add('store_orders', 'sonata_type_collection', array(
          'allow_add' => false
        ))
    

    which gives an error 'The option "allow_add" does not exist'

        ->add('store_orders', 'sonata_type_collection', array(
          'type_options' => array('allow_add' => false)
        ))
    

    which also gives an error 'The option "allow_add" does not exist'

    I'd also like to remove the delete checkboxes next to each item in the collection. I presume the answer to that lies in a similar area.

    Any assistance would be greatly appreciated.

  • user1207727
    user1207727 about 12 years
    Yeah I can't quite believe they don't have an option to turn these things off. I've asked the same question in their google group but they're not overly keen to respond given the number of unanswered questions on there. Such a shame it's so poorly documented as it seems to be so popular and powerful.
  • user1207727
    user1207727 about 12 years
    Sorry, I meant to add that I'm not keen to override the templates as I'll probably want the add feature in other modules. For now I'll hold out a little longer for a response
  • Bobulous
    Bobulous almost 10 years
    Please add an explanation of what your suggestion is doing, to help people who find your answer to understand how it works.