简体   繁体   中英

Sonata Admin: How to remove “Add New” button from dashboard only?

I'm using Symfony 2.7 with Sonata Admin Bundle to manage some products and product images. I used the Sonata Admin Cookbook recipe: https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_file_uploads.html for images.

Because an image must have a product id associated with it, I want to disable the "Add New" Image link from the Sonata admin dashboard and from the top toolbar, so any uploaded image will have an associated product. Actually the only place where images should be allowed to be added is in the product add/edit page.

I've tried to remove the route like this, according to some answers found here: Sonata Admin Dashboard: configure actions per entity

protected function configureRoutes(RouteCollection $collection)
{
    $container = $this->getConfigurationPool()->getContainer(); 

    if ($container->get('request')->get('_route') == 'sonata_admin_dashboard') {
        $collection->remove('create');
    }
}

But this solution is not good, because, if the cache is initialized when I access the admin dashboard, the route gets removed everywhere, but if the cache is initialized on a different page, then the route will be present on all pages, including dashboard, because Sonata Admin validates in templates if the route exists when displaying the link.

So, I need the route to exist and to remove the link. Can this be done using configuration or I have to rewrite the templates?

In your admin class :

use Sonata\AdminBundle\Route\RouteCollection;

protected function configureRoutes(RouteCollection $collection)
    {
        $collection->remove('create');
    }

You can also remove Delete, Show etc ...

Check : https://sonata-project.org/bundles/admin/master/doc/reference/routing.html#removing-a-single-route

Try this in the admin class:

public function getDashboardActions() {
    $actions = parent::getDashboardActions();
    unset($actions['create']);
    return $actions;
}

In the following you can see a list of options to hide Sonatadmin functions:

protected function configureRoutes(RouteCollection $collection)
{
    $collection->remove('create');
    $collection->remove('edit');
    $collection->remove('delete');
    $collection->remove('show');
    $collection->remove('export');
}

In your sonata admin configuration where you define your admins, remove the "group" tag. It looks something like this.

services:
    sonata.admin.images:
        class: AppBundle\Admin\ImageAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Content", label: "Manage images" }
        arguments:
            ...

(remove group: "Content" or whatever you have your group set as)

This will put your image admin in a separate block called "default".

Then, explicitly define which blocks you show on the dashboard, leaving out "default":

sonata_admin:
    dashboard:
        groups:
            Content: ~
            AnotherGroup: ~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM