Creating breadcrumbs in symfony 2.1 using the knpmenu package

What is the best way to create breadcrumbs using the knpmenu package in symfony 2.1.x? In addition to using 3 party packages.

UPDATE:

Hi, theunraveler , sorry for the late reply. Now I am following your example, and I am stuck at one point. Here the code below throws an exception that

Missing argument 2 for Acme\DemoBundle\Menu\MenuBuilder::getBreadCrumbs() {% set item = knp_menu_get('main') %} {{ knp_menu_render(item) }} {% block breadcrumbs %} {% set breadcrumbs = knp_menu_get('breadcrumbs', [], {'request': app.request, 'menu': item }) %} {{ dump(breadcrumbs) }} {% endblock %} 

Why doesn't it accept the variable "item"?

+4
source share
3 answers

The Knp\Menu\MenuItem class has a getBreadcrumbsArray() method. It should return an array of items in the current active menu. If you are using an earlier version of KnpMenu (<= 1.1.2, I think), the returned array will look like label => uri . Otherwise, it will be an array with each element having the keys label , uri and item .

To find the current menu item, you probably want to create a method in your controller (or somewhere else, if that makes sense for your project), looks something like this:

 public function getCurrentMenuItem($menu) { foreach ($menu as $item) { if ($item->isCurrent()) { return $item; } if ($item->getChildren() && $current_child = $this->getCurrentMenuItem($item)) { return $current_child; } } return null; } 

From there, you can call getBreadcrumbsArray() on the return value:

 $this->getCurrentMenuItem($your_menu)->getBreadcrumbsArray(); 

I assume that I would eventually create a Twig extension that registers the global breadcrumbs , and put the getCurrentMenuItem() method in there. This way you can have the breadcrumb variable in all of your templates without having to manually display it in each controller.

Source: https://github.com/KnpLabs/KnpMenu/blob/master/src/Knp/Menu/MenuItem.php#L544 .

+3
source

Since version 2.0 , getBreadcrumbsArray been ported to Knp\Menu\Util\MenuManipulator .

A possible workout for this solution is to create a branch extension:

 <?php namespace Kimwild\CommonBundle\Twig; use Knp\Menu\Util\MenuManipulator; use Knp\Menu\ItemInterface; class MenuManipulatorExtension extends \Twig_Extension { public function getFunctions() { return array( new \Twig_SimpleFunction('menu_manipulator', array($this, 'menuManipulator')), ); } public function menuManipulator(ItemInterface $item) { $manipulator = new MenuManipulator(); return $manipulator->getBreadcrumbsArray($item); } public function getName() { return 'menu_manipulator'; } } 

Register branch extension:

 kimwild_common.menu_manipulator_extension: class: Kimwild\CommonBundle\Twig\MenuManipulatorExtension public: false tags: - { name: twig.extension } 

In breadcrumb.html.twig:

 {% block root %} {%- for link in menu_manipulator(item) %} /* whatever you want to do ... */ {%- endfor %} {% endblock %} 
+10
source

Since KnpMenu 2.1 , a new twig function appears: knp_menu_get_breadcrumbs_array

You can take a look at my point: https://gist.github.com/fsevestre/b378606c4fd23814278a

I added a new twp function knp_menu_get_current_item that retrieves the current menu item and works fine using the knp_menu_get_breadcrumbs_array function.

-

Edit:

With KnpMenu 2.2 you can do the following:

 <ol class="breadcrumb"> {% for breadcrumb_item in knp_menu_get_breadcrumbs_array(knp_menu_get_current_item('main')) %} {% 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> 

https://github.com/KnpLabs/KnpMenu/blob/master/doc/02-Twig-Integration.markdown#functions

The knp_menu_get_current_item('main') Twig function will retrieve the current menu item for the main menu.

+2
source

Source: https://habr.com/ru/post/1445735/


All Articles