Substituted old menu trough KNP menu bundle

This commit is contained in:
Kevin Frantz 2018-09-06 11:02:22 +02:00
parent 36b9020c01
commit 8e031de88e
8 changed files with 276 additions and 57 deletions

View File

@ -17,7 +17,7 @@ class DefaultController extends AbstractController implements DefaultControllerI
*/ */
public function imprint(): Response public function imprint(): Response
{ {
return $this->render("standard/imprint.html.twig",['menu_items'=>[]]); return $this->render("standard/imprint.html.twig");
} }
/** /**
@ -25,7 +25,7 @@ class DefaultController extends AbstractController implements DefaultControllerI
*/ */
public function homepage(): Response public function homepage(): Response
{ {
return $this->render("standard/homepage.html.twig",['menu_items'=>[]]); return $this->render("standard/homepage.html.twig");
} }
} }

View File

@ -17,7 +17,7 @@ class UserController extends AbstractController implements UserControllerInterfa
*/ */
public function logout(): Response public function logout(): Response
{ {
return $this->render("user/login.html.twig",['menu_items'=>[]]); return $this->render("user/login.html.twig");
} }
/** /**
@ -25,7 +25,7 @@ class UserController extends AbstractController implements UserControllerInterfa
*/ */
public function login(): Response public function login(): Response
{ {
return $this->render("user/login.html.twig",['menu_items'=>[]]); return $this->render("user/login.html.twig");
} }
/** /**
@ -33,7 +33,6 @@ class UserController extends AbstractController implements UserControllerInterfa
*/ */
public function register(): Response public function register(): Response
{ {
return $this->render("user/register.html.twig",['menu_items'=>[]]); return $this->render("user/register.html.twig");
} }
} }

View File

@ -30,7 +30,11 @@ class Menu
public function userTopbar(RequestStack $request): ItemInterface public function userTopbar(RequestStack $request): ItemInterface
{ {
$menu = $this->factory->createItem('root'); $menu = $this->factory->createItem('root', array(
'childrenAttributes' => array(
'class' => 'navbar-nav mr-auto',
),
));
$this->dispatcher->dispatch( $this->dispatcher->dispatch(
UserMenuEvent::EVENT, UserMenuEvent::EVENT,

View File

@ -1,9 +1,5 @@
<?php <?php
// src/Subscriber/UserMenuSubscriber.php
namespace App\Subscriber; namespace App\Subscriber;
use App\Entity\User;
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;
@ -11,44 +7,91 @@ use Symfony\Component\Translation\TranslatorInterface;
class UserMenuSubscriber implements EventSubscriberInterface class UserMenuSubscriber implements EventSubscriberInterface
{ {
/** /**
*
* @var TokenStorageInterface * @var TokenStorageInterface
*/ */
private $tokenStorage; private $tokenStorage;
/** /**
*
* @var TranslatorInterface * @var TranslatorInterface
*/ */
private $translator; private $translator;
public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator) public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
{ {
$this->tokenStorage = $tokenStorage; $this->tokenStorage = $tokenStorage;
$this->translator = $translator; $this->translator = $translator;
} }
public function onUserMenuConfigure(UserMenuEvent $event) public function onUserMenuConfigure(UserMenuEvent $event): void
{ {
$menu = $event->getItem(); $menu = $event->getItem();
/** @var User $user */ $menu->addChild(
$user = $this->tokenStorage->getToken()->getUser(); 'linking',
[
$dropdown = $menu->addChild( 'route' => 'homepage',
$this->translator->trans('Hello %username%', ['%username%' => 'Noname'], 'usermenu'), ]
['dropdown' => true]
); );
$menu->addChild(
'texting',
[
'labelAttributes' => [
'class' => 'class3 class4',
],
]
);
$dropdown = $menu->addChild(
'Hello Me',
[
'attributes' => [
'dropdown' => true,
],
]
);
$dropdown->addChild( $dropdown->addChild(
$this->translator->trans('Login', [], 'usermenu'), 'Profile',
['route' => 'user_login'] [
'route' => 'homepage',
'attributes' => [
'divider_append' => true,
],
]
);
$dropdown->addChild(
'text',
[
'attributes' => [
'icon' => 'fa fa-user-circle',
],
'labelAttributes' => [
'class' => ['class1', 'class2'],
],
]
);
$dropdown->addChild(
'Logout',
[
'route' => 'user_logout',
'attributes' => [
'divider_prepend' => true,
'icon' => 'fa fa-sign-out',
],
]
); );
} }
public static function getSubscribedEvents(): array public static function getSubscribedEvents(): array
{ {
return [ return [
UserMenuEvent::EVENT => 'onUserMenuConfigure', UserMenuEvent::EVENT => 'onUserMenuConfigure'
]; ];
} }
} }

View File

@ -1,7 +1,6 @@
{% extends "base.html.twig" %} {% extends "base.html.twig" %}
{% block body %} {% block body %}
{% include 'frames/structure/navbar.html.twig' with menu_items %} {% include 'frames/structure/navbar.html.twig'%}
{% include 'topbar.html.twig' %}
<br /> <br />
<div class="container"> <div class="container">
{% block content %} {% block content %}

View File

@ -0,0 +1,200 @@
{# @see https://gist.github.com/lsv/24a9e9d7d961d22226f8e220d2c4a7d0 #}
{% extends 'knp_menu.html.twig' %}
{% macro setCssClassAttribute(item, type, add) %}
{% set getter = 'get' ~ type %}
{% set setter = 'set' ~ type %}
{% set value = attribute(item, getter, ['class']) %}
{% if value is iterable %}
{% set value = value|join(' ') %}
{% endif %}
{% do attribute(item, setter, ['class', value ~ ' ' ~ add]) %}
{% endmacro %}
{% block item %}
{% import "knp_menu.html.twig" as macros %}
{#
As multiple level is not currently supported by bootstrap 4
This requires you to install
https://github.com/bootstrapthemesco/bootstrap-4-multi-dropdown-navbar
And set the the use_multilevel = true
#}
{% set use_multilevel = false %}
{% if item.displayed %}
{%- set attributes = item.attributes %}
{%- set is_dropdown = attributes.dropdown|default(false) %}
{%- set divider_prepend = attributes.divider_prepend|default(false) %}
{%- set divider_append = attributes.divider_append|default(false) %}
{# unset bootstrap specific attributes #}
{%- set attributes = attributes|merge({'dropdown': null, 'divider_prepend': null, 'divider_append': null }) %}
{%- if divider_prepend %}
{{ block('dividerElement') }}
{%- endif %}
{# building the class of the item #}
{%- set classes = item.attribute('class') is not empty ? [item.attribute('class'), 'nav-item'] : ['nav-item'] %}
{%- if matcher.isCurrent(item) %}
{%- set classes = classes|merge([options.currentClass]) %}
{%- elseif matcher.isAncestor(item, options.depth) %}
{%- set classes = classes|merge([options.ancestorClass]) %}
{%- endif %}
{%- if item.actsLikeFirst %}
{%- set classes = classes|merge([options.firstClass]) %}
{%- endif %}
{%- if item.actsLikeLast %}
{%- set classes = classes|merge([options.lastClass]) %}
{%- endif %}
{# building the class of the children #}
{%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %}
{%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %}
{# adding classes for dropdown #}
{%- if is_dropdown %}
{%- set classes = classes|merge(['dropdown']) %}
{%- set childrenClasses = childrenClasses|merge(['dropdown-menu']) %}
{%- endif %}
{# putting classes together #}
{%- if classes is not empty %}
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
{%- endif %}
{%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %}
<li{{ macros.attributes(attributes) }}>
{# displaying the item #}
{%- if is_dropdown %}
{{ block('dropdownElement') }}
{%- elseif item.uri is not empty and (not item.current or options.currentAsLink) %}
{{ block('linkElement') }}
{%- else %}
{{ block('spanElement') }}
{%- endif %}
{%- if divider_append %}
{{ block('dividerElement') }}
{%- endif %}
{% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
{{ block('dropdownlinks') }}
{% endif %}
</li>
{% endif %}
{% endblock %}
{% block dropdownlinks %}
{% if use_multilevel %}
<ul class="dropdown-menu">
{% else %}
<div class="dropdown-menu">
{% endif %}
{% for item in item.children %}
{{ block('renderDropdownlink') }}
{% if use_multilevel and item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
{{ block('dropdownlinks') }}
{% endif %}
{% endfor %}
{% if not use_multilevel %}
</div>
{% else %}
</ul>
{% endif %}
{% endblock %}
{% block renderDropdownlink %}
{% import _self as ownmacro %}
{%- set divider_prepend = item.attributes.divider_prepend|default(false) %}
{%- set divider_append = item.attributes.divider_append|default(false) %}
{%- set attributes = item.attributes|merge({'dropdown': null, 'divider_prepend': null, 'divider_append': null }) %}
{% if use_multilevel %}
<li>
{% endif %}
{%- if divider_prepend %}
{{ block('dividerElementDropdown') }}
{%- endif %}
{%- if item.uri is not empty and (not item.current or options.currentAsLink) %}
{{ ownmacro.setCssClassAttribute(item, 'LinkAttribute', 'dropdown-item') }}
{{ block('linkElement') }}
{%- else %}
{{ block('spanElementDropdown') }}
{%- endif %}
{%- if divider_append %}
{{ block('dividerElementDropdown') }}
{%- endif %}
{% if use_multilevel %}
</li>
{% endif %}
{% endblock %}
{% block spanElementDropdown %}
{% import "knp_menu.html.twig" as macros %}
{% import _self as ownmacro %}
{{ ownmacro.setCssClassAttribute(item, 'LabelAttribute', 'dropdown-header') }}
<div {{ macros.attributes(item.labelAttributes) }}>
{% if item.attribute('icon') is not empty %}
<i class="{{ item.attribute('icon') }}"></i>
{% endif %}
{{ block('label') }}
</div>
{% endblock %}
{% block dividerElementDropdown %}
<div class="dropdown-divider"></div>
{% endblock %}
{% block dividerElement %}
{% if item.level == 1 %}
<li class="divider-vertical"></li>
{% else %}
<li class="divider"></li>
{% endif %}
{% endblock %}
{% block linkElement %}
{% import "knp_menu.html.twig" as macros %}
{% import _self as ownmacro %}
{{ ownmacro.setCssClassAttribute(item, 'LinkAttribute', 'nav-link') }}
<a href="{{ item.uri }}"{{ macros.attributes(item.linkAttributes) }}>
{% if item.attribute('icon') is not empty %}
<i class="{{ item.attribute('icon') }}"></i>
{% endif %}
{{ block('label') }}
</a>
{% endblock %}
{% block spanElement %}
{% import "knp_menu.html.twig" as macros %}
{% import _self as ownmacro %}
{{ ownmacro.setCssClassAttribute(item, 'LabelAttribute', 'navbar-text') }}
<span {{ macros.attributes(item.labelAttributes) }}>
{% if item.attribute('icon') is not empty %}
<i class="{{ item.attribute('icon') }}"></i>
{% endif %}
{{ block('label') }}
</span>
{% endblock %}
{% block dropdownElement %}
{% import "knp_menu.html.twig" as macros %}
{%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %}
{%- set classes = classes|merge(['dropdown-toggle', 'nav-link']) %}
{%- set attributes = item.linkAttributes %}
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
{%- set attributes = attributes|merge({'data-toggle': 'dropdown'}) %}
<a href="#"{{ macros.attributes(attributes) }}>
{% if item.attribute('icon') is not empty %}
<i class="{{ item.attribute('icon') }}"></i>
{% endif %}
{{ block('label') }}
<b class="caret"></b>
</a>
{% endblock %}
{% block label %}{{ item.label|trans }}{% endblock %}

View File

@ -7,29 +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">
<ul class="navbar-nav mr-auto"> {{ knp_menu_render('userTopbar', {'currentClass': 'active', 'template': 'frames/structure/knp_menu.html.twig'}) }}
{% for item in menu_items %}
{% if item.name %}
{% if item.url %}
<li class="nav-item {% if item.active %}active{% endif %}"><a
class="nav-link" href="{{ item.url }}"> {{ item.name }}
</a></li>
{% elseif item.toArray %}
<li class="nav-item dropdown"><a
class="nav-link dropdown-toggle" href="#" id="navbarDropdown"
role="button" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false"> {{ item.name }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
{%for link in item %}
<a class="dropdown-item {% if link.active %}active{% endif %}"
href="{{ link.url }}"> {{ link.name }}
</a>
{% endfor %}
</div></li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
</div> </div>
</nav> </nav>

View File

@ -1,3 +0,0 @@
{# templates/topbar.htm.twig #}
{{ knp_menu_render('userTopbar') }}