4

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

Dashboard menu items


Items in sidebar menu Side bar menu

ro ko
  • 2,906
  • 3
  • 37
  • 58
  • But your screenshot shows the sidebar with the entry ... or what do you want to achieve? – Jim Panse Jul 11 '18 at 09:23
  • @JimPanse there is one entry Benutzer (User) but I'd like the list to have Neu (new) and Liste (list) as seen in dashboard menu (one with arrows). – ro ko Jul 11 '18 at 09:35
  • Ahhh ... now i understand ... one possibility is to use the ConfigureMenuEvent to grab the menu entry and modify as you want. Maybe you have to inject the route builder to generate edit/list routes – Jim Panse Jul 11 '18 at 09:38

1 Answers1

4

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.

revengeance
  • 851
  • 7
  • 21
  • How to pass parameters from the parameters.yml to this listener? – samar Apr 27 '20 at 13:38
  • If you want to add link to existing group - you can do it using code like $event->getMenu()->getChild('ExistingGroupName')->addChild(... Also sonata by default use font-awesome icons – Andrew Zhilin Jun 10 '20 at 16:25