src/EventSubscriber/MenuBuilderSubscriber.php line 37
<?php/** This file is part of the Tabler-Bundle demo.* Copyright 2021 Kevin Papst - www.kevinpapst.de** For the full copyright and license information, please view the LICENSE* file that was distributed with this source code.*/namespace App\EventSubscriber;use KevinPapst\TablerBundle\Event\MenuEvent;use KevinPapst\TablerBundle\Model\MenuItemInterface;use KevinPapst\TablerBundle\Model\MenuItemModel;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;class MenuBuilderSubscriber implements EventSubscriberInterface{private $security;private $environment;public function __construct(AuthorizationCheckerInterface $security, string $environment){$this->security = $security;$this->environment = $environment;}public static function getSubscribedEvents(): array{return [MenuEvent::class => ['onSetupNavbar', 100],];}public function onSetupNavbar(MenuEvent $event): void{$event->addItem(new MenuItemModel('home', 'Home', 'home', [], 'fas fa-home'));$forms = new MenuItemModel('users', 'Users', null, [], 'fas fa-users');$forms->addChild(new MenuItemModel('user', 'List', 'user.index', [], 'fas fa-user-friends'));$forms->addChild(new MenuItemModel('user_add', 'Add', 'user.new', [], 'fas fa-user-plus'));$event->addItem($forms);$forms = new MenuItemModel('surveys', 'Surveys', null, [], 'fas fa-laptop');$forms->addChild(new MenuItemModel('survey', 'List', 'survey.index', [], 'fas fa-list'));$event->addItem($forms);$forms = new MenuItemModel('categories', 'Categories', null, [], 'fas fa-laptop');$forms->addChild(new MenuItemModel('category', 'List', 'category.index', [], 'fas fa-list'));$forms->addChild(new MenuItemModel('category_add', 'Add', 'category.new', [], 'fas fa-user-plus'));// $forms->addChild(// new MenuItemModel('modelsurvey', 'Add', 'modelsurvey.new', [], 'fas fa-plus-square')// );$event->addItem($forms);$report = new MenuItemModel('report', 'Reports', 'report.index', [], 'fas fa-tachometer-alt');$report->setBadge('New');$report->setBadgeColor('green');$event->addItem($report);if ($this->security->isGranted('IS_AUTHENTICATED')) {$event->addItem(new MenuItemModel('logout', 'Logout', 'security_logout', [], 'fas fa-sign-out-alt'));}$this->activateByRoute($event->getRequest()->get('_route'),$event->getItems());}/*** @param string $route* @param MenuItemInterface[] $items*/protected function activateByRoute(string $route, array $items): void{foreach ($items as $item) {if ($item->hasChildren()) {$this->activateByRoute($route, $item->getChildren());} elseif ($item->getRoute() == $route) {$item->setIsActive(true);}}}}