简体   繁体   中英

SonataAdmin, Add a item to the side menu

I am trying to add the items/route to the side menu, basically I have User which has list and add functionality listed in dashboard, now I would like to have these under my sidebar menu too.

I registered a service:

#config/services.yml
    admin.user:
            class: AdminBundle\Admin\UserAdmin
            arguments: [~, AppBundle\Entity\User, AdminBundle:UserAdmin]
            tags:
                - { name: sonata.admin, manager_type: orm, group: admin, label: User }
            calls:
                - [ setAuthorizationChecker, ['@security.authorization_checker']]

Followed by configuration for dashboard.

sonata_admin:
    templates:
        dashboard: 'SonataAdminBundle:Core:dashboard.html.twig'
        layout:   'AdminBundle::standard_layout.html.twig'
        user_block: 'AdminBundle:Core:user_block.html.twig'
    title: 'Book-a-slot<br /><span>Admin panel</span>'
    title_logo: bundles/app/images/logo.png
    dashboard:
        groups:
            user:
                label: User
                items:
                    - admin.user
        blocks:
            -
                position: left
                type: sonata.admin.block.admin_list

Looked into configureTabMenu if I can add through it but no luck.

protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, ['edit', 'show'])) {
            return;
        }

        $menu->addChild(
            'User Create',
            [
                'uri' => $this->generateUrl(UserAdmin::class.'.create'),
            ]
        );

    }

Items in Dashboard

仪表板菜单项


Items in sidebar menu 侧栏菜单

You can check the name of your routes by doing php bin/console debug:router. But, I suppose it's.

  • admin_admin_user_list (for list) - but i don't know why you need it, because default menu entry forwards to list
  • admin_admin_user_create (for creating user)

So what's next? You must register menuBuilderListener. (We will hook into process of menu creation and there you will have full control)

Services.yml

app.menu_admin:
    class: AdminBundle\EventListener\MenuBuilderListener
    tags:
        - { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: adminMenuItems }

Next create folder for EventListeners and create MenuBuilderListener there. I will just copy paste one of my listeners, I have used recently. (adjust to your needs).

<?php

namespace AdminBundle\EventListener;

use Sonata\AdminBundle\Event\ConfigureMenuEvent;

/**
 * Class MenuBuilderListener
 * @package AdminBundle\EventListener
 */
class MenuBuilderListener
{
    /**
     * @param ConfigureMenuEvent $event
     */
    public function adminMenuItems(ConfigureMenuEvent $event)
    {
        $event->getMenu()
            ->addChild(
                'dashboard',
                [
                    'route' => 'admin_dashboard',
                ]
            )
            ->setExtras(
                [
                    'icon' => '<span class="menu-ico mif mif-chart-pie"></span>&nbsp;',
                ]
            )
            ->setLabel('Dashboard')
            ->getParent()
            ->addChild(
                'reviews',
                [
                    'route' => 'admin_reviews',
                ]
            )
            ->setExtras(
                [
                    'icon' => '<span class="mif mif-bubble"></span>&nbsp;',
                ]
            )
            ->setLabel('Reviews')
            ->getParent()
            ->addChild('pages')
            ->setExtras(
                [
                    'icon' => '<span class="mif mif-files-empty"></span>&nbsp;',
                ]
            )
            ->setLabel('Pages')
            ->addChild('home', ['route' => 'admin_pages_home'])
            ->setExtras(
                [
                    'icon' => '<span class="mif mif-file-empty"></span>&nbsp;',
                ]
            )
            ->setLabel('Home')
            ->getParent()
            ->addChild('review', ['route' => 'admin_pages_review'])
            ->setExtras(
                [
                    'icon' => '<span class="mif mif-file-empty"></span>&nbsp;',
                ]
            )
            ->setLabel('Review')
            ->getParent()
            ->addChild('about_us', ['route' => 'admin_pages_about_us'])
            ->setExtras(
                [
                    'icon' => '<span class="mif mif-file-empty"></span>&nbsp;',
                ]
            )
            ->setLabel('About Us')
            ->getParent()
            ->getParent();
    }
}

That's all.

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