簡體   English   中英

Sonata Admin:如何僅從儀表板中刪除“添加新”按鈕?

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

我正在使用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');
    }
}

但是這個解決方案並不好,因為,如果在我訪問管理儀表板時初始化緩存,則路徑會在任何地方被刪除,但如果緩存在不同的頁面上初始化,則路由將出現在所有頁面上,包括儀表板,因為如果在顯示鏈接時路徑存在,Sonata Admin會在模板中驗證。

所以,我需要存在的路由並刪除鏈接。 這可以使用配置完成,還是我必須重寫模板?

在您的管理類中:

use Sonata\AdminBundle\Route\RouteCollection;

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

您也可以刪除刪除,顯示等...

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

在admin類中嘗試此操作:

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

在下面,您可以看到隱藏Sonatadmin函數的選項列表:

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

在您定義管理員的sonata管理員配置中,刪除“group”標簽。 它看起來像這樣。

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

(刪除group: "Content"或您組中設置的任何group: "Content"

這會將您的圖像管理員放在一個名為“默認”的單獨塊中。

然后,明確定義您在儀表板上顯示的塊,省略“默認”:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM