Sonata Admin:如何仅从仪表板中删除"添加新"按钮?

VMC*_*VMC 13 php dashboard configuration-files symfony sonata-admin

我正在使用Symfony 2.7和Sonata Admin Bundle来管理一些产品和产品图像.我使用了Sonata Admin Cookbook食谱:https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_file_uploads.html用于图像.

由于图片必须具有与之关联的产品ID,因此我想禁用Sonata管理信息中心和顶部工具栏中的"添加新图片"链接,因此任何上传的图片都会包含相关产品.实际上,唯一允许添加图像的地方是产品添加/编辑页面.

根据这里找到的一些答案,我试图删除这样的路线:Sonata Admin Dashboard:为每个实体配置操作

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

    if ($container->get('request')->get('_route') == 'sonata_admin_dashboard') {
        $collection->remove('create');
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这个解决方案并不好,因为如果在我访问管理仪表板时初始化缓存,则路径会在任何地方被删除,但如果缓存在不同的页面上初始化,则路由将出现在所有页面上,包括仪表板,因为如果在显示链接时路径存在,Sonata Admin会在模板中验证.

所以,我需要存在的路由并删除链接.这可以使用配置完成,还是我必须重写模板?

Kai*_*are 12

在您的管理类中:

use Sonata\AdminBundle\Route\RouteCollection;

protected function configureRoutes(RouteCollection $collection)
{
    $collection->remove('create');
}
Run Code Online (Sandbox Code Playgroud)

您也可以删除删除,显示等...

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

  • 这根本删除了“添加”/“创建”按钮。 (2认同)
  • 例如,这还会删除集合表单字段中的创建按钮,而不仅仅是仪表板 (2认同)

小智 7

在admin类中尝试此操作:

public function getDashboardActions() {
    $actions = parent::getDashboardActions();
    unset($actions['create']);
    return $actions;
}
Run Code Online (Sandbox Code Playgroud)

  • 此方法的名称是 SonataAdminBundle 3.4+ 中的 getActionButtons (2认同)

小智 6

在下面,您可以看到隐藏 Sonatadmin 功能的选项列表:

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


ama*_*ert 0

在您定义管理员的奏鸣曲管理员配置中,删除“组”标签。它看起来像这样。

services:
    sonata.admin.images:
        class: AppBundle\Admin\ImageAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Content", label: "Manage images" }
        arguments:
            ...
Run Code Online (Sandbox Code Playgroud)

(删除group: "Content"或您为组设置的任何内容)

这会将您的图像管理放在一个名为“默认”的单独块中。

然后,明确定义在仪表板上显示哪些块,省略“默认”:

sonata_admin:
    dashboard:
        groups:
            Content: ~
            AnotherGroup: ~
Run Code Online (Sandbox Code Playgroud)