General menu optimation

This commit is contained in:
Kevin Frantz 2018-09-17 17:43:26 +02:00
parent edfaa58e2e
commit 4fc812ac64
4 changed files with 31 additions and 28 deletions

View File

@ -1,20 +1,23 @@
<?php <?php
namespace App\Subscriber; namespace App\Subscriber;
use App\Event\Menu\Topbar\UserMenuEvent; use App\Event\Menu\Topbar\UserMenuEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
use Knp\Menu\ItemInterface;
class UserMenuSubscriber implements EventSubscriberInterface class UserMenuSubscriber implements EventSubscriberInterface
{ {
/** /**
*
* @var TokenStorageInterface * @var TokenStorageInterface
*/ */
private $tokenStorage; private $tokenStorage;
/** /**
*
* @var TranslatorInterface * @var TranslatorInterface
*/ */
private $translator; private $translator;
@ -31,64 +34,64 @@ class UserMenuSubscriber implements EventSubscriberInterface
$menu->addChild('start', [ $menu->addChild('start', [
'route' => 'homepage', 'route' => 'homepage',
'attributes' => [ 'attributes' => [
'icon' => 'fab fa-font-awesome-flag', 'icon' => 'fab fa-font-awesome-flag'
],
] ]
); ]);
$menu->addChild( $menu->addChild('imprint', [
'imprint',
[
'route' => 'imprint', 'route' => 'imprint',
'attributes' => [ 'attributes' => [
'icon' => 'fas fa-address-card', 'icon' => 'fas fa-address-card'
],
] ]
); ]);
$this->generateUserDropdown($menu);
}
private function generateUserDropdown(ItemInterface $menu): void
{
$dropdown = $menu->addChild($this->tokenStorage->getToken() $dropdown = $menu->addChild($this->tokenStorage->getToken()
->getUsername() ?? 'user', [ ->getUsername() ?? 'user', [
'attributes' => [ 'attributes' => [
'dropdown' => true, 'dropdown' => true,
'icon' => 'fas fa-user', 'icon' => 'fas fa-user'
], ]
]); ]);
if ($this->tokenStorage->getToken()->getRoles()) { if ($this->tokenStorage->getToken()->getRoles()) {
$dropdown->addChild('logout', [ $dropdown->addChild('logout', [
'route' => 'logout', 'route' => 'logout',
'attributes' => [ 'attributes' => [
'icon' => 'fas fa-sign-out-alt', 'icon' => 'fas fa-sign-out-alt',
'divider_append' => true, 'divider_append' => true
], ]
]); ]);
$dropdown->addChild('edit profile', [ $dropdown->addChild('edit profile', [
'route' => 'fos_user_profile_edit', 'route' => 'fos_user_profile_edit',
'attributes' => [ 'attributes' => [
'icon' => 'fas fa-user-edit', 'icon' => 'fas fa-user-edit',
'divider_append' => true, 'divider_append' => true
], ]
]); ]);
} else { } else {
$dropdown->addChild('login', [ $dropdown->addChild('login', [
'route' => 'fos_user_security_login', 'route' => 'fos_user_security_login',
'attributes' => [ 'attributes' => [
'divider_append' => true, 'divider_append' => true,
'icon' => 'fas fa-sign-in-alt', 'icon' => 'fas fa-sign-in-alt'
], ]
]); ]);
} }
$dropdown->addChild('register', [ $dropdown->addChild('register', [
'route' => 'fos_user_registration_register', 'route' => 'fos_user_registration_register',
'attributes' => [ 'attributes' => [
'icon' => 'fas fa-file-signature', 'icon' => 'fas fa-file-signature'
], ]
]); ]);
} }
public static function getSubscribedEvents(): array public static function getSubscribedEvents(): array
{ {
return [ return [
UserMenuEvent::EVENT => 'onUserMenuConfigure', UserMenuEvent::EVENT => 'onUserMenuConfigure'
]; ];
} }
} }

View File

@ -1,6 +1,6 @@
{% extends "base.html.twig" %} {% extends "base.html.twig" %}
{% block body %} {% block body %}
{% include 'frames/structure/navbar.html.twig'%} {% include 'frames/structure/navbar/navbar_top.html.twig'%}
<br /> <br />
<div class="container"> <div class="container">
{% include 'frames/structure/message/flash_messages.html.twig'%} {% include 'frames/structure/message/flash_messages.html.twig'%}

View File

@ -7,6 +7,6 @@
<span class="navbar-toggler-icon"></span> <span class="navbar-toggler-icon"></span>
</button> </button>
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
{{ knp_menu_render('userTopbar', {'currentClass': 'active', 'template': 'frames/structure/knp_menu.html.twig'}) }} {{ knp_menu_render('userTopbar', {'currentClass': 'active', 'template': 'frames/structure/navbar/knp_menu.html.twig'}) }}
</div> </div>
</nav> </nav>