SonataAdminBundle 侧边栏的嵌套菜单项

Nested menu items at SonataAdminBundle sidebar

我想像树一样嵌套菜单项。我目前有这样的配置:

dashboard:
    groups:
        company:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Company
            items:
                - sonata.admin.company
                - sonata.admin.brand
                - sonata.admin.media
                - sonata.admin.message
        territory:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Territory
            items:
                - sonata.admin.territory
                - sonata.admin.target
        reps:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Representative
            items:
                - sonata.admin.representative
                - sonata.admin.email
                - sonata.admin.targetbrand
                - sonata.admin.territorybrand

我想要这样的东西:

dashboard:
    groups:
        company:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Company
            label_catalogue: PDOneBundle
            items:
                - sonata.admin.company
                    - sonata.admin.brand
                        - sonata.admin.media
                        - sonata.admin.message
        territory:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Territory
            label_catalogue: PDOneBundle
            items:
                - sonata.admin.territory
                     - sonata.admin.target
        reps:
            icon: <i class="fa fa-lg fa-fw fa fa-cogs"></i>
            label: Representative
            label_catalogue: PDOneBundle
            items:
                - sonata.admin.representative
                    - sonata.admin.email

我该怎么做?可能吗?

更新

基于 docs by SonataAdminBundle and recommended by @hugo-briand I made some changes by extending the menu 所以我在 PDI\PDOneBundle\EventListener\MenuBuilderListener.php 下创建了一个文件,如下所示:

namespace PDI\PDOneBundle\EventListener;
use Sonata\AdminBundle\Event\ConfigureMenuEvent;

class MenuBuilderListener
{
    public function createMainMenu(ConfigureMenuEvent $event)
    {
        $menu = $event->getMenu();

            $menu
                ->addChild('Dashboard', array('uri' => '/'))
                ->setAttribute('icon', 'fa fa-home');

            $menu
                ->addChild('Company', array())
                ->setAttribute('icon', 'fa fa-inbox')
                ->addChild('Company', array(
                   'route' => 'admin_pdi_pdone_company_list',
                ))
                ->setAttribute('icon', 'fa fa-inbox')
                ->getParent()
                ->addChild('Brand', array(
                    'route' => 'admin_pdi_pdone_brand_list',
                ))
                ->setAttribute('icon', 'fa fa-inbox')
                ->addChild('Media', array(
                    'route' => 'admin_pdi_pdone_media_list',
                ))
                ->setAttribute('icon', 'fa fa-inbox')
                ->getParent();
    }
}

然后我在PDI\PDOneBundle\Resources\config\services.yml注册监听如下:

services:
    app.menu_listener:
        class: PDI\PDOneBundle\EventListener\MenuBuilderListener
        tags:
            - { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: createMainMenu }

但无法正常工作,因为相同的默认菜单仍在显示。有什么建议吗?

是的,你可以。但并不完全像您指定的那样。

SonataAdmin现在集成了KnpMenu来生成侧边菜单。它记录在这里(https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_knp_menu.html);不过请注意使用主版本,此版本尚未作为稳定版本发布。

注意:由于文档尚未重新发布,因此缺少一些信息。不过,您可以直接在 Github 存储库中看到它:https://github.com/sonata-project/SonataAdminBundle/blob/41328b42f91bf4c72153cdb1fc2c50a5092c7755/Resources/doc/cookbook/recipe_knp_menu.rst

对于菜单中的每个项目,您现在可以提供一个 KnpMenu 提供程序来生成您的菜单,并覆盖关联的布局。