Skip to content

Instantly share code, notes, and snippets.

@guiyomh
Forked from fsevestre/ README.md
Created July 24, 2016 21:39
Show Gist options
  • Save guiyomh/763151ac7ac5412cb14856fbb11f01d9 to your computer and use it in GitHub Desktop.
Save guiyomh/763151ac7ac5412cb14856fbb11f01d9 to your computer and use it in GitHub Desktop.
Symfony 2 breadcrumbs based on KnpMenuBundle (KnpMenu 2.1)
{{ knp_menu_render('main', {'template': ':Menu:breadcrumb.html.twig'}) }}
<ol class="breadcrumb">
{% for breadcrumb_item in knp_menu_get_breadcrumbs_array(knp_menu_get_current_item(item)) %}
{% if not loop.last %}
<li><a href="{{ breadcrumb_item.uri }}">{{ breadcrumb_item.label }}</a></li>
{% else %}
<li class="active">{{ breadcrumb_item.label }}</li>
{% endif %}
{% endfor %}
</ol>
<?php
namespace AppBundle\Menu;
use Knp\Menu\FactoryInterface;
class Builder
{
/**
* @var FactoryInterface
*/
private $factory;
/**
* @param FactoryInterface $factory
*/
public function __construct(FactoryInterface $factory)
{
$this->factory = $factory;
}
/**
* @param array $options
*
* @return \Knp\Menu\ItemInterface
*/
public function createMainMenu(array $options)
{
$menu = $this->factory->createItem('Dashboard', ['route' => 'dashboard']);
$menu->addChild('Foo', ['route' => 'foo_list']);
$menu['Foo']->addChild('Create Foo', ['route' => 'foo_create', 'display' => false]);
$menu->addChild('Bar', ['route' => 'bar_list']);
$menu['Bar']->addChild('Create Bar', ['route' => 'bar_create', 'display' => false]);
return $menu;
}
}
<?php
namespace AppBundle\Twig;
use Knp\Menu\ItemInterface;
use Knp\Menu\Twig\Helper;
use Knp\Menu\Matcher\MatcherInterface;
class MenuExtension extends \Twig_Extension
{
/**
* @var Helper
*/
private $helper;
/**
* @var MatcherInterface
*/
private $matcher;
/**
* @param Helper $helper
* @param MatcherInterface $matcher
*/
public function __construct(Helper $helper, MatcherInterface $matcher)
{
$this->helper = $helper;
$this->matcher = $matcher;
}
/**
* @return array
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('knp_menu_get_current_item', array($this, 'getCurrentItem')),
);
}
/**
* Retrieves the current item.
*
* @param ItemInterface|string $menu
*
* @return ItemInterface
*/
public function getCurrentItem($menu)
{
$rootItem = $this->helper->get($menu);
$currentItem = $this->retrieveCurrentItem($rootItem);
if (null === $currentItem) {
$currentItem = $rootItem;
}
return $currentItem;
}
/**
* @param ItemInterface $item
*
* @return ItemInterface|null
*/
private function retrieveCurrentItem(ItemInterface $item)
{
$currentItem = null;
if ($this->matcher->isCurrent($item)) {
return $item;
}
if ($this->matcher->isAncestor($item)) {
foreach ($item->getChildren() as $child) {
$currentItem = $this->retrieveCurrentItem($child);
if (null !== $currentItem) {
break;
}
}
}
return $currentItem;
}
/**
* @return string
*/
public function getName()
{
return 'menu';
}
}
services:
app.menu.builder:
class: AppBundle\Menu\Builder
arguments:
- '@knp_menu.factory'
tags:
- { name: knp_menu.menu_builder, method: createMainMenu, alias: main }
app.twig.menu_extension:
class: AppBundle\Twig\MenuExtension
arguments:
- '@knp_menu.helper'
- '@knp_menu.matcher'
tags:
- { name: twig.extension }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment