Skip to content

Commit eca4f68

Browse files
committed
Added a Menu Extension to KnpMenuBundle for cleaner generation of Navbars.
Changed mopa_bootstrap_navbar to be a pass-through for knp_menu_render with bootstrap options.
1 parent dd8c90a commit eca4f68

File tree

10 files changed

+208
-173
lines changed

10 files changed

+208
-173
lines changed

DependencyInjection/Compiler/NavbarPass.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

DependencyInjection/MopaBootstrapExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function load(array $configs, ContainerBuilder $container)
3434
$yamlloader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3535
$yamlloader->load('twig_extensions.yml');
3636
$yamlloader->load("form_extensions.yml");
37-
37+
3838
if (isset($config['form'])) {
3939
foreach ($config['form'] as $key => $value) {
4040
if (is_array($value)) {

MopaBootstrapBundle.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
use Symfony\Component\HttpKernel\Bundle\Bundle;
66
use Symfony\Component\DependencyInjection\ContainerBuilder;
77
use Mopa\Bundle\BootstrapBundle\DependencyInjection\Compiler\FormPass;
8-
use Mopa\Bundle\BootstrapBundle\DependencyInjection\Compiler\NavbarPass;
98

109
class MopaBootstrapBundle extends Bundle
1110
{
1211
public function build(ContainerBuilder $container)
1312
{
1413
parent::build($container);
1514
$container->addCompilerPass(new FormPass());
16-
$container->addCompilerPass(new NavbarPass());
1715
}
1816
}

Navbar/Factory/NavbarExtension.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Mopa\Bundle\BootstrapBundle\Navbar\Factory;
4+
5+
use Knp\Menu\Factory\ExtensionInterface;
6+
use Knp\Menu\ItemInterface;
7+
8+
class NavbarExtension implements ExtensionInterface
9+
{
10+
public function buildItem(ItemInterface $item, array $options)
11+
{
12+
if ($options['navbar']) {
13+
$item->setChildrenAttribute('class', 'nav navbar-nav');
14+
}
15+
16+
if ($options['subnavbar']) {
17+
$item->setChildrenAttribute('class', 'nav nav-pills');
18+
}
19+
20+
if ($options['dropdown_header']) {
21+
$item
22+
->setAttribute('role', 'presentation')
23+
->setAttribute('class', 'dropdown-header')
24+
->setUri(null);
25+
}
26+
27+
if ($options['dropdown']) {
28+
$item
29+
->setUri('#')
30+
->setAttribute('class', 'dropdown')
31+
->setLinkAttribute('class', 'dropdown-toggle')
32+
->setLinkAttribute('data-toggle', 'dropdown')
33+
->setChildrenAttribute('class', 'dropdown-menu')
34+
->setChildrenAttribute('role', 'menu');
35+
36+
if ($options['caret']) {
37+
$item->setExtra('caret', 'true');
38+
}
39+
}
40+
41+
if ($options['divider']) {
42+
$item
43+
->setLabel('')
44+
->setUri(null)
45+
->setAttribute('role', 'presentation')
46+
->setAttribute('class', 'divider');
47+
}
48+
49+
if ($options['push_right']) {
50+
$class = $item->getChildrenAttribute('class', '');
51+
$item->setChildrenAttribute('class', $class . ' pull-right');
52+
}
53+
54+
if ($options['icon']) {
55+
$item->setExtra('icon', $options['icon']);
56+
}
57+
}
58+
59+
public function buildOptions(array $options)
60+
{
61+
return array_merge(array(
62+
'navbar' => false,
63+
'subnavbar' => false,
64+
'dropdown_header' => false,
65+
'dropdown' => false,
66+
'caret' => false,
67+
'push_right' => false,
68+
'icon' => false,
69+
'divider' => false,
70+
), $options);
71+
}
72+
}

Navbar/Twig/NavbarExtension.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22

33
namespace Mopa\Bundle\BootstrapBundle\Navbar\Twig;
44

5-
use Mopa\Bundle\BootstrapBundle\Navbar\Renderer\NavbarRenderer;
5+
use Knp\Menu\Twig\Helper;
66

77
class NavbarExtension extends \Twig_Extension
88
{
9-
protected $renderer;
9+
protected $helper;
10+
1011
/**
11-
* @param \Mopa\Bootstrap\Menu\Renderer\NavbarRenderer $renderer
12+
* @param \Knp\Menu\Twig\Helper $helper
1213
*/
13-
public function __construct(NavbarRenderer $renderer)
14+
public function __construct(Helper $helper)
1415
{
15-
$this->renderer = $renderer;
16+
$this->helper = $helper;
1617
}
1718

1819
public function getFunctions()
1920
{
2021
return array(
21-
'mopa_bootstrap_navbar' => new \Twig_Function_Method($this, 'render', array('is_safe' => array('html'))),
22+
'mopa_bootstrap_navbar' => new \Twig_Function_Method($this, 'renderNavbar', array('is_safe' => array('html'))),
2223
);
2324
}
2425

@@ -30,9 +31,16 @@ public function getFunctions()
3031
* @param string $renderer
3132
* @return string
3233
*/
33-
public function render($name, array $options = array(), $renderer = null)
34+
public function renderNavbar($menu, array $options = array(), $renderer = null)
3435
{
35-
return $this->renderer->renderNavbar($name, $options, $renderer);
36+
$options = array_merge(array(
37+
'template' => 'MopaBootstrapBundle:Menu:menu.html.twig',
38+
'currentClass' => 'active',
39+
'ancestorClass' => 'active',
40+
'allow_safe_labels' => true,
41+
), $options);
42+
43+
return $this->helper->render($menu, $options, $renderer);
3644
}
3745

3846
/**
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
parameters:
2-
mopa_bootstrap.navbar.generic: Mopa\Bundle\BootstrapBundle\Navbar\GenericNavbar
3-
41
services:
5-
mopa_bootstrap.navbar_renderer:
6-
class: Mopa\Bundle\BootstrapBundle\Navbar\Renderer\NavbarRenderer
7-
arguments: [ @service_container, [] ]
8-
tags:
9-
# The alias is what is used to retrieve the menu
10-
- { name: knp_menu.renderer, alias: navbar }
11-
122
mopa_bootstrap.navbar.twig.extension:
133
class: Mopa\Bundle\BootstrapBundle\Navbar\Twig\NavbarExtension
14-
arguments: [ @mopa_bootstrap.navbar_renderer ]
4+
arguments: [ "@knp_menu.helper" ]
155
tags:
166
- { name: twig.extension }
7+
8+
mopa_bootstrap.navbar.menu.extension:
9+
class: Mopa\Bundle\BootstrapBundle\Navbar\Factory\NavbarExtension
10+
tags:
11+
- { name: knp_menu.factory_extension, priority: -100 }

0 commit comments

Comments
 (0)