Skip to content

Instantly share code, notes, and snippets.

@merk
Created May 7, 2012 03:15
Show Gist options
  • Save merk/2625661 to your computer and use it in GitHub Desktop.
Save merk/2625661 to your computer and use it in GitHub Desktop.
Event driven menus
<div class="nav-collapse">
{{ knp_menu_render('navigation', {
'ancestorClass': 'active',
'currentClass': 'active'
}) }}
<form class="pull-right navbar-search action="{# todo search url #}">
<input id="quick-search" type="text" class="search-query span2" placeholder="Search" />
</form>
{{ knp_menu_render('right_navigation', {
'ancestorClass': 'active',
'currentClass': 'active'
}) }}
</div>
<?php
/*
* TODO: Copyright notices on Infinite Networks files.
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au/>
*/
namespace Infinite\MenuBundle\Menu;
use Infinite\MenuBundle\Event\ConfigureMenuEvent;
use Infinite\MenuBundle\Events;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
class Menu
{
private $dispatcher;
private $factory;
public function __construct(EventDispatcherInterface $dispatcher, FactoryInterface $factory)
{
$this->dispatcher = $dispatcher;
$this->factory = $factory;
}
public function navigation(Request $request)
{
$menu = $this->factory->createItem('root');
$menu->setCurrentUri($request->getRequestUri());
$menu->setChildrenAttribute('class', 'nav');
$event = new ConfigureMenuEvent($this->factory, $menu);
$this->dispatcher->dispatch(Events::CONFIGURE, $event);
return $event->getMenu();
}
public function rightNavigation(Request $request)
{
$menu = $this->factory->createItem('root');
$menu->setCurrentUri($request->getRequestUri());
$menu->setChildrenAttribute('class', 'nav pull-right');
$event = new ConfigureMenuEvent($this->factory, $menu);
$this->dispatcher->dispatch(Events::CONFIGURE_RIGHT, $event);
return $event->getMenu();
}
}
<?php
namespace Ibms\CustomerBundle\EventListener;
use Infinite\MenuBundle\Event\ConfigureMenuEvent;
use Infinite\MenuBundle\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MenuListener implements EventSubscriberInterface
{
public function configureMenu(ConfigureMenuEvent $event)
{
$menu = $event->getMenu();
$customers = $menu->addChild('Customers', array(
'uri' => '#',
'attributes' => array(
'class' => 'dropdown'
),
'linkAttributes' => array(
'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown',
),
'childrenAttributes' => array(
'class' => 'dropdown-menu',
),
));
$customers->addChild('Customers', array(
'route' => 'ibms_customer_home',
));
$customers->addChild('Groups', array(
'route' => 'ibms_customer_group_home',
));
}
static function getSubscribedEvents()
{
return array(
Events::CONFIGURE => array('configureMenu', 512),
);
}
}
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="infinite_menu.builder" class="Infinite\MenuBundle\Menu\Menu">
<argument type="service" id="event_dispatcher" />
<argument type="service" id="knp_menu.factory" />
</service>
<service id="infinite_menu.menu.navigation" class="Knp\Menu\MenuItem" factory-service="infinite_menu.builder" factory-method="navigation" scope="request">
<argument type="service" id="request" />
<tag name="knp_menu.menu" alias="navigation" />
</service>
<service id="infinite_menu.menu.right_navigation" class="Knp\Menu\MenuItem" factory-service="infinite_menu.builder" factory-method="rightNavigation" scope="request">
<argument type="service" id="request" />
<tag name="knp_menu.menu" alias="right_navigation" />
</service>
</services>
</container>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment