Disable 'add' with sonata_type_collection in SonataAdminBundle formats

Does anyone know how I can get rid of the green pluses that allow a new item to be added to the collection in the form of a sonata annotation? The native collectiontype has allow_add and allow_delete, but sonata_type_collection doesn't seem to notice these options.

I've 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 the error "The parameter 'allow_add' does not exist"

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

      

which also gives the error "Parameter 'allow_add' does not exist"

I would also like to remove the delete checkboxes next to each item in the collection. I believe the answer to this question lies in a similar area.

Any help would be greatly appreciated.

+3


source to share


4 answers


try it

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

      



When you add a collection to Sonata annotation forms, the Add New button is displayed by default, so as not to display the Add New or + button, set the add_btn key to FALSE in the array which is the third parameter in the add function.

+10


source


I'm not very good with SonataAdminBundle, but two options will help.

First you need to use a collection of types 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.

Another option is to override the template with one of yours.

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 {% block collection_widget%}

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

+1


source


Edit: Just try

->add('store_orders', null)

      

instead

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

      


Sonata provides the following ROLE based access to objects: -

ROLE_SONATA _..._ GUEST: the guest who is allowed to view the object and the list of objects; ROLE_SONATA _..._ PERSONNEL: probably the largest part of the users, the state user has the same rights as guests, and additionally is allowed to EDIT and CREATE new objects; ROLE_SONATA _..._ EDITOR: The editor is given all access and, compared to staff users, is allowed to DELETE AND EXPORT

ROLE_SONATA _..._ ADMIN: All access is granted to the administrator and in addition the user is allowed to grant other users access.

Most likely your user will be assigned the following access controls as STAFF (who logged in with 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 to each class.

If you want to get rid of the green pluses, you just need to remove ROLE - "ROLE _ .. NAME_CREATE" for the appropriate administrator.

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

0


source


1) You can actually create a template extension and then only use it for a specific field, so you won't be overriding the default template anyway.

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

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

      

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

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

      

The above code says "Show the template list_custom.html.twig for this field." The default template will be used in all cases where you do not specify a template override.

See the following code: 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 if it works.

configureRoute(RouteCollection $collection) {
  $collection->remove('edit');
} 

      

0


source







All Articles