Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create breadcrumb with KNPMenu 2.1? #224

Closed
qweluke opened this issue Oct 10, 2015 · 1 comment
Closed

How to create breadcrumb with KNPMenu 2.1? #224

qweluke opened this issue Oct 10, 2015 · 1 comment

Comments

@qweluke
Copy link

qweluke commented Oct 10, 2015

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

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.op1']);
    $menu['Dashboard']->addChild('Details 2', ['route' => 'app.admin.dashboard.op2']);

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

How I can create a breadcumb to current rotue if I'm at /dashboard/details?
I want to create a breadcumb which looks like this: home / dashboard / details /
Last month you've added knp_menu_get_breadcrumbs_array. Should I use this function? Can you share an example with me?

I'm trying to use it in that way:

 {% set item = knp_menu_get('AppBundle:AdminMenuBuilder:sidebarMenu') %}
 {% set breadcrumbs = knp_menu_get_breadcrumbs_array(item, [ 'Details']) %}
 {% for breadcrumb in breadcrumbs %}
   {{ dump(breadcrumb) }}
 {% endfor %}
@jonny827
Copy link

jonny827 commented May 19, 2016

@qweluke Your Menu does not contain valid ItemInterfaces. Check the doc's again to ensure you properly using the addchild() function parameters correctly. Once you do this then use the following code for a string based lowercased breadcrumbs for your menu based on your current item being Details.

    use Knp\Menu\Util\MenuManipulator;  

    $menuManipulator = new MenuManipulator();

/*  Returns a string as you never said you wanted breadcrumb links 
    If you want links thencopy the below function and build the links in your forked function ( getPathAsBreadcrumbLinks ) */

    $breadcrumbs = $menuManipulator->getPathAsString($menu['Details'], ' / ');

    //Lowercase String: Your example requested lowercase however your Labels were uppercase
    $breadcrumbs = strtolower($breadcrumbs);

    echo $breadcrumbs;

You can also keep up with the discussion about this topic with a current PR Breadcrumb renderer #161

@dbu dbu closed this as completed Jun 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants