koftikes/symfony-adminlte-bundle

Sidebar Menu Items

djbb7 opened this issue · 9 comments

djbb7 commented

Hey Koftikes,

Thanks for creating the bundle, it's working as described.

I have a question though, what's the right way to add items to the navigation sidebar?

Best,
Daniel

Can you explain this more. What exactly you want to do?

djbb7 commented

Hey. I was trying to add buttons to the navigation sidebar. The way I did it was by creating a custom service that subscribes to the sbs.admin_lte.sidebar_menu event, and adds each MenuItemModel by calling the addItem method of the event.

Is this the correct approach? Thanks

Yep, I create documentetion in the near future

@djbb7 Can you post a code example of the service file and config of your solution to this, please? I am having trouble adding sidebar items and can't quite figure it out. I ask as there is no documentation on this just yet.

Thanks in advance

djbb7 commented

Hi @Eightcap, sorry for the delayed reply, have you managed to figure it out?

If not, here is how I did it. First I added the service to my src/AppBundle/Resources/config/services.yml file. I am injecting the @security.authorization_checker dependency because I want to display a different menu for the admin. You might also want to inject the user information or entity manager depending on what you want to do.

    sbs_adminlte.sidebar_menu_listener:
        class: AppBundle\ThemeEvent\SidebarMenuEventListener
        arguments: ['@security.authorization_checker']
        tags:
            - { name: kernel.event_listener, event: sbs.admin_lte.sidebar_menu, method: onShowMenu }

Then implement the SidebarMenuEventListener inside your bundle:

<?php

namespace AppBundle\ThemeEvent;

use SbS\AdminLTEBundle\Event\SidebarMenuEvent;
use SbS\AdminLTEBundle\Model\MenuItemModel;
use SbS\AdminLTEBundle\Model\MenuItemInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;


class SidebarMenuEventListener
{
    protected $authorizationChecker;

    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;
    }

    /**
     * @param SidebarMenuEvent $event
     */
    public function onShowMenu(SidebarMenuEvent $event)
    {
        foreach ($this->getMenu() as $index=>$item) {
            $item->setId($index);
            $event->addItem($item);
        }
    }

    /**
     * @return array
     */
    protected function getMenu()
    {
        $label_main      = new MenuItemModel('MENU');

        $label_routes  = (new MenuItemModel('Routes'))->setRoute('route_index')
            ->setIcon('fa fa-map-signs');
        $label_planes  = (new MenuItemModel('Planes'))->setRoute('plane_index')
            ->setIcon('fa fa-bus');
        $label_schedules  = (new MenuItemModel('Flight Schedules'))->setRoute('schedule_index')
            ->setIcon('fa fa-calendar');
        $label_bookings  = (new MenuItemModel('Bookings'))->setRoute('booking_index')->setRouteArgs(array('d'=>'today'))
            ->setIcon('fa fa-book');

        $item_multilevel = (new MenuItemModel('Bookings'))
            ->setIcon('fa fa-book')
            ->addChild((new MenuItemModel('New Booking'))->setRoute('booking_new'))
            ->addChild((new MenuItemModel('Cancel Booking'))->setRoute('booking_cancel_search'))
            ->addChild((new MenuItemModel('Today\'s Bookings'))->setRoute('booking_index')->setRouteArgs(array('d'=>'today')))
            ->addChild((new MenuItemModel('All Bookings'))->setRoute('booking_index'))
            ->addChild((new MenuItemModel('Cancelled Bookings'))->setRoute('booking_index_cancellations'));

        $label_payments  = (new MenuItemModel('Payments'))->setRoute('payment_index')
            ->setIcon('fa fa-money');
        $label_manifest  = (new MenuItemModel('Passenger Manifest'))->setRoute('departures_index')
            ->setIcon('fa fa-address-book-o');

        $label_manager = new MenuItemModel(('MANAGER'));
        $label_offices  = (new MenuItemModel('Offices'))->setRoute('office_index')
            ->setIcon('fa fa-building-o');
        $label_accounts  = (new MenuItemModel('Accounts'))->setRoute('account_index')
            ->setIcon('fa fa-users');
        $label_reports  = (new MenuItemModel('Reports'))->setRoute('account_index')
            ->setIcon('fa fa-line-chart');

        $menu = [
            $label_main,
            $label_routes,
            $label_planes,
            $label_schedules,
            $label_bookings,
            $label_payments,
            $label_manifest,
            $item_multilevel
        ];

        if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
            $menu = array_merge($menu, [$label_manager,
                $label_offices,
                $label_accounts,
                $label_reports]);
        }

        return $menu;
    }
}

As you can see there are different levels of navigation possible.

Let me know if this is clear.

@djbb7 Thank you so much. That worked perfectly. 👍

djbb7 commented

Good. I'm closing this issue, and this remains here for anyone who runs into the same problem in the future.

hello friend, how do I close the parents for defects??

This helped a lot. Thanks.