4

So I have a menu built as shown in the example below:

<?php namespace AppBundle\Menu;

use Doctrine\ORM\EntityManager;
use Knp\Menu\FactoryInterface;
use Knp\Menu\MenuFactory;

class AdminMenuBuilder
{

    public function sidebarMenu(FactoryInterface $factory, array $options)
    {
    $menu = $factory->createItem('root', array(
        'navbar' => true,
        'childrenAttributes' => [
            'class' => 'nav main-menu',
        ],
    ));


    $menu->addChild('Dashboard')
         ->setAttributes([
            'icon' =>'fa fa-dashboard',
            'class' => 'dropdown',
             'dropdown' => true
         ]);

    $menu['Dashboard']->addChild('Details', ['route' => 'app.admin.dashboard']);
    $menu['Dashboard']->addChild('Details 2', ['route' => 'app.admin.dashboard']);

    $menu->addChild('Users', ['route' => 'app.admin.dashboard.users'])
        ->setAttribute('icon', 'fa fa-users');


        return $menu;
    }
}

How can I create a breadcumb using KNPMenuBundle v2? I'm using symfony2 2.7.5

breq
  • 24,412
  • 26
  • 65
  • 106
  • I don't understand what seems to be the problem here? You just need to create a custom layout ([docs](http://symfony.com/doc/current/bundles/KnpMenuBundle/index.html#step-3-optional-configure-the-bundle))? – tftd Oct 09 '15 at 20:51
  • Template will be enought? I've read that Iin 1.2 it looks like this http://stackoverflow.com/questions/17347694/symfony-2-breadcrumbs-with-knpmenu but i can't find info for 2.x version – breq Oct 09 '15 at 20:59
  • Try this solution (pay attention to the comments) https://gist.github.com/Korpch/4183696 – tftd Oct 09 '15 at 21:00

2 Answers2

3

KnpMenuBundle 2.1.0 (released some weeks ago) has a knp_menu_get_breadcrumbs_array() function which you can use. For instance:

{% set item = knp_menu_get('some_menu_item') %}
{% set breadcrumbs = knp_menu_get_breadcrumbs_array(item) %}
{% for breadcrumb in breadcrumbs %}
  <a href="{{ breadcrumb.url }}">{{ breadcrumb.label }}</a>
{% endfor %}
Wouter J
  • 41,455
  • 15
  • 107
  • 112
0

If you have nested menu the above solution won't work. Here is the extension that returns array of active menu items recursively:

<?php

namespace AnalyticsBundle\Twig;

use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\MatcherInterface;
use Knp\Menu\Twig\Helper;

/**
 * Class MenuExtension
 * @package AnalyticsBundle\Twig
 */
class MenuExtension extends \Twig_Extension
{
    private $helper;

    private $matcher;

    public function __construct(Helper $helper, MatcherInterface $matcher)
    {
        $this->helper = $helper;
        $this->matcher = $matcher;
    }

    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('analytics_get_current_menu_items', function($menu, array $path = array(), array $options = array()) {
                $menu = $this->helper->get($menu, $path, $options);
                return $this->getCurrentItems($menu);
            }),
        ];
    }

    private function getCurrentItems(ItemInterface $item)
    {
        $items = [];
        $getActiveItems = function (ItemInterface $item) use (&$getActiveItems, &$items) {
            if ($this->matcher->isCurrent($item)) {
                $items[] = $item;
                foreach ($item->getChildren() as $child) {
                    $getActiveItems($child);
                }
            }
        };
        $getActiveItems($item);
        return $items;
    }

    public function getName()
    {
        return 'analytics_menu';
    }
}

Example usage:

                <ul class="page-breadcrumb">
                    {% for item in analytics_get_current_menu_items('main') %}
                        <li>
                            <span>{{ item.label }}</span>
                        {% if not loop.last %}
                            |
                        {% endif %}
                        </li>
                    {% endfor %}
                </ul>
Cmyker
  • 2,318
  • 1
  • 26
  • 29