Sonataadminbundle: Sonata type model - hide/delete the "create" button

Created on 31 Jan 2012  路  22Comments  路  Source: sonata-project/SonataAdminBundle

Hello!
http://clip2net.com/s/1xx2h
How, I can hide or delete the "create" button?

Most helpful comment

If you are using something like sonata_type_model (subforms) and need to hide some routes only for that complex form you can use somethink like this

  protected function configureRoutes(RouteCollection $collection)
      {
        parent::configureRoutes($collection);

        if($this->hasParentFieldDescription()) {
                $collection
          //        ->remove('show')
                  ->remove('create')
          //        ->remove('delete')
          //        ->remove('edit')
                ;
        }
      }

Just in case it helps somebody else.

All 22 comments

Hi!

In your PizzaAdmin.php file, add
use Sonata\AdminBundle\Route\RouteCollection;

Then for example if you want to prevent the create, edit and delete actions :

use Sonata\AdminBundle\Route\RouteCollection;


class PizzaAdmin extends Admin
{
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection
            ->remove('create')
            ->remove('delete')
            ->remove('edit')
            ;

    }
    public function configureShowFields(ShowMapper $showMapper)
    { 
    // .....
    }
    // etc
}

Then the buttons will disappear (because the routes are removed, and if you look at the templates in twig, it checks if the route exists before displaying the button).

Thanks!

@IgorN Please close this issue, if your problem is solved.

If you are using something like sonata_type_model (subforms) and need to hide some routes only for that complex form you can use somethink like this

  protected function configureRoutes(RouteCollection $collection)
      {
        parent::configureRoutes($collection);

        if($this->hasParentFieldDescription()) {
                $collection
          //        ->remove('show')
                  ->remove('create')
          //        ->remove('delete')
          //        ->remove('edit')
                ;
        }
      }

Just in case it helps somebody else.

Thanks @rodrigobb, that really helped me!

How to do if i want only to Hide the button and not to remove the create route/action ?
Think about a parent relation where you want to show the parent but disable the Add New from create|edit form.

@piribes +1 How to do this?

and how to prevent redirect to the edit page after created a new entity when edit routing has been removed ? (which will lead to Exception unable to find the route sonata.admin.xxxxxx.edit)

Thanks to @rodrigobb, that was a great help! Was tearing my hair out (and I don't have much) trying to find an "option" to disable the "add new" when embedding one Admin in another.

@rodrigobb You are the men!!! Thanks!
@piribes may be you can override template and remove button

@piribes - I think @rodrigobb's method does what you want. It only removes the route when the admin appears as a child. You can still actually use the routes if you go to the relevant URL.

I have seen a few people out there wondering that there is no equivalent to 'allow_add' => false to simply hide the button:
http://stackoverflow.com/questions/9689396/disable-add-with-sonata-type-collection-in-sonataadminbundle-forms

This would be an improvement, don't you think?

BTW:
I tried the first example but the button would not disappear when using sonata_type_model_autocomplete?

class ContractAdmin extends Admin
{
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection
            ->remove('create')
//            ->remove('delete')
//            ->remove('edit')
            ;
    }

    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('branch', 'sonata_type_model_autocomplete', array(
                'property' => 'name',
                'minimum_input_length' => 2,
                'label' => false
                ));
    }
}

sonata_type_model has a parameter btn_add. You can set it to false to hide the create button.

sonata_type_model_autocomplete does not have any button to create new item yet.

Documentation of Sonata form types

Thanks @pulzarraider . Are there any plans for the sonata_type_model_autocomplete create button - any issue number to follow?

@webdevilopers I have no time for that now.
Please, create new issue for this feature (we are discussing in closed isssue). I (or someone else) will improve it in the future.

Thanks!!!!
It Works..

How can I remove a route depending on the user who's logged in. I want to remove the create route from my userAdmin so that people can't add users. Only the super admin should be able to add users.

I've tried this;

protected function configureRoutes(RouteCollection $collection)
    {
        $securityContext = $this->getConfigurationPool()->getContainer()->get('security.authorization_checker');

        if (!$securityContext->isGranted('ROLE_SUPER_ADMIN')) {
            $collection->remove('create');
            $collection->remove('edit');
        }
    }

But I get an error;

The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.

I can't think that there is no firewall configured.

Because this works to hide specific fields from my forms for only super admin in the same class.

@Mentos93 - I think you're coming from the wrong angle. You don't remove the route for different users, just set the user permissions sensibly. E.g. if your general users do not have "CREATE" permissions on this object then they will not see any links to add/create and if they guess the URL they will be bounced.

See: https://sonata-project.org/bundles/admin/3-x/doc/reference/security.html

@caponica I am completely aware of that, but I don't use the SonataUserBundle. I only use FOSUserBundle directly with SonataAdminBundle with Symfony 3. So I don't know another way.

Or will it work without SonataUserBundle as well?

@Mentos93 - however your users are being authenticated, they will have some kind of permissions. If they have ROLE_SONATA_ADMIN_YOURADMIN_CREATE then they will see the add/create links and be able to access the route. If they do not have this persmission then they will not.

However, I suggest you open a ticket on stackoverflow and post a link to it here. The git tickets are not really the place for individual questions like this.

@caponica OK, will do, thanks.

@ghost I'm facing same issue as you mentioned. Have you solved it?

Was this page helpful?
0 / 5 - 0 ratings