4

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
  • 1,543
  • 1
  • 15
  • 18

4 Answers4

14

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.

Albert St Clair
  • 141
  • 1
  • 7
  • 1
    Please add an explanation of what your suggestion is doing, to help people who find your answer to understand how it works. – Bobulous May 04 '14 at 22:47
1

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.

Pierre
  • 1,553
  • 12
  • 22
  • 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 Mar 13 '12 at 22:02
  • 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 – user1207727 Mar 13 '12 at 22:04
0

Edit: Just try

->add('store_orders', null)

instead of

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

Sonata gives the following ROLE based access to objects:-

ROLE_SONATA_..._GUEST: a guest that is allowed to view an object and a list of objects; ROLE_SONATA_..._STAFF: probably the biggest part of the users, a staff user has the same permissions as guests and is additionally allowed to EDIT and CREATE new objects; ROLE_SONATA_..._EDITOR: an editor is granted all access and, compared to the staff users, is allowed to DELETE and EXPORT

ROLE_SONATA_..._ADMIN: an administrative user is granted all access and on top of that, the user is allowed to grant other users access.

Most likely the following access controls will be assigned to your user with role as STAFF (who has logged into Sonata Admin)

'ROLE_.._NAME__EDIT',
    'ROLE_.._NAME__LIST',
    'ROLE_.._NAME__CREATE',
    'ROLE_.._NAME__VIEW',
    'ROLE_.._NAME__DELETE',
    'ROLE_.._NAME__OPERATOR',
    'ROLE_.._NAME__MASTER',

These roles will be assigned for each and every class.

If you want to get rid of the "Green pluses", you just need to remove the ROLE - 'ROLE_..NAME_CREATE' for the corresponding Admin Entity.

Please refer to this part of the documentation if you get stuck.

Amit
  • 3,644
  • 9
  • 38
  • 49
0

1) You can actually create a template extension and then only use it for a specific field, so you don't end up overriding the default template in all cases.

If you want to do it this way, basically you are going to want to create a new template like user1207727 suggested above, making sure to extend the template you want:

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

Then include it on your form for just the field you want to remove the add button on:

$listMapper
->add('custom', 'string', array('template' => 'YourBundle:YourDirectory:your_template.html.twig'))

The above code says "show the list_custom.html.twig template for this field". The default template will be used in all cases that you don't specify a template override.

See this code for a further example: https://github.com/sonata-project/SonataMediaBundle/blob/2.0/Admin/BaseMediaAdmin.php

2) According to this post: http://groups.google.com/group/sonata-users/browse_thread/thread/6a94d662c8a6a17f you can also remove the route to remove the add button. I haven't tried this so I'm not sure it works.

configureRoute(RouteCollection $collection) {
  $collection->remove('edit');
} 
adavea
  • 1,535
  • 1
  • 19
  • 25