Created
February 8, 2012 15:51
-
-
Save havvg/1770582 to your computer and use it in GitHub Desktop.
Symfony2 KnpMenu TwitterBootstrap switch user menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Ormigo\Bundle\BackofficeBundle\Menu; | |
use Knp\Menu\FactoryInterface; | |
use Symfony\Component\DependencyInjection\ContainerAware; | |
use Ormigo\Bundle\UserBundle\Model\User\UserQuery; | |
use Symfony\Component\Security\Core\Role\SwitchUserRole; | |
class Builder extends ContainerAware | |
{ | |
public function mainMenu(FactoryInterface $factory) | |
{ | |
$menu = $factory->createItem('root'); | |
$menu->setCurrentUri($this->container->get('request')->getRequestUri()); | |
$menu->addChild('Dashboard', array( | |
'uri' => '#', | |
)); | |
$menu->addChild('Demo View', array( | |
'route' => 'demo', | |
)); | |
return $menu; | |
} | |
public function switchUserMenu(FactoryInterface $factory) | |
{ | |
$menu = $factory->createItem('root'); | |
$menu->setAttributes(array( | |
'class' => 'pull-right', | |
'dropdown' => true, | |
'divider_append' => true, | |
)); | |
$menu->setLabel('Switch User'); | |
$securityContext = $this->container->get('security.context'); | |
if ($securityContext->isGranted('ROLE_ALLOWED_TO_SWITCH') or $securityContext->isGranted('ROLE_PREVIOUS_ADMIN')) { | |
// The current (may be switched) username. | |
$username = $securityContext->getToken()->getUser()->getUsername(); | |
// The actual user, if switched, retrieve the correct one. | |
$actualUser = $securityContext->getToken()->getUser(); | |
foreach ($securityContext->getToken()->getRoles() as $role) { | |
if ($role instanceof SwitchUserRole) { | |
$actualUser = $role->getSource(); | |
break; | |
} | |
} | |
foreach (UserQuery::create()->find() as $eachUser) { | |
// only show links to different users | |
if ($username !== $eachUser->getUsername()) { | |
$menu->addChild($eachUser->getUsername(), array('uri' => '?_switch_user='.$eachUser->getUsername())); | |
} | |
} | |
$menu->addChild($actualUser->getUsername(), array('uri' => '?_switch_user=_exit')); | |
} | |
return $menu; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
twitter_bootstrap.menu_renderer: | |
class: Ormigo\Bundle\BackofficeBundle\Menu\TwitterBootstrapRenderer | |
arguments: | |
- @twig | |
- 'BackofficeBundle:Menu:menu_bootstrap.html.twig' | |
tags: | |
- { name: knp_menu.renderer, alias: bootstrap } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Ormigo\Bundle\BackofficeBundle\Menu; | |
use Knp\Menu\ItemInterface; | |
use Knp\Menu\Renderer\TwigRenderer; | |
class TwitterBootstrapRenderer extends TwigRenderer | |
{ | |
public function render(ItemInterface $item, array $options = array()) | |
{ | |
$options = array_merge( | |
array('currentClass' => 'active'), | |
$options | |
); | |
if ('root' === $item->getName()) { | |
$item->setAttribute('class', trim('nav '.$item->getAttribute('class'))); | |
} | |
return parent::render($item, $options); | |
} | |
} |
last missing bit is the example how to actually render the menu.
{{ knp_menu_render('main', {}, 'bootstrap') }}
more info in documentation:
https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/doc/custom_renderer.md
@fralex:
services.yml
should have one more argument, @knp_menu.matcher
services:
twitter_bootstrap.menu_renderer:
class: Ormigo\Bundle\BackofficeBundle\Menu\TwitterBootstrapRenderer
arguments:
- @twig
- 'BackofficeBundle:Menu:menu_bootstrap.html.twig'
- @knp_menu.matcher
tags:
- { name: knp_menu.renderer, alias: bootstrap }
in fact, you don't need custom renderer at all. you can achieve this just in menu builder:
$sub = $menu->addChild('Sub', array(
'uri' => '#',
'linkAttributes' => array(
'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown',
),
'childrenAttributes' => array(
'class' => 'dropdown-menu',
),
'attributes' => array(
'class' => 'dropdown',
),
));
foreach ($subMenuItems as $item) {
$sub->addChild($item['name'], array(
'route' => $item['route'],
));
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Thanks for the great code.
If I register my service, I get the following error:
What is wrong?
What KnpMenuBundle version you use?
fralex