Skip to content

BreadCrumb Array with Refactored MenuItem #194

Closed
@peterrehm

Description

@peterrehm

With the refactoring @stof commited yesterday to Knp/Menu the functionality I discussed earlier in here about the breadcrumb is not working any more:

Builder:

<php
     /**
     * Breadcrumb builder
     *
     * @param FactoryInterface $factory
     * @param array            $options
     *
     * @return \Knp\Menu\MenuItem
     */
    public function mainBreadcrumb(FactoryInterface $factory, array $options)
    {

        //TODO: Match the current according to parts of the route if concrete menus are being shown
        //TODO: MainMenu is rendered twice which could cause negative performance impacts
        $menu = $this->mainMenu($factory, $options);

        $matcher = $this->container->get('knp_menu.matcher');
        $voter = $this->container->get('core.menu.voter.router');
        $matcher->addVoter($voter);

        $treeIterator = new \RecursiveIteratorIterator(
            new \Knp\Menu\Iterator\RecursiveItemIterator(
                new \ArrayIterator(array($menu))
            ),
            \RecursiveIteratorIterator::SELF_FIRST
        );

        $iterator = new \Knp\Menu\Iterator\CurrentItemFilterIterator($treeIterator, $matcher);

        // Set Current as an empty Item in order to avoid exceptions on knp_menu_get
        $current = new \Knp\Menu\MenuItem('', $factory);

        foreach ($iterator as $item) {
            $item->setCurrent(true);
            $current = $item;
            break;
        }

        return $current;
    }

Main Template:

{% set breadCrumbItem = knp_menu_get('CoreBundle:Builder:mainBreadcrumb') %}
{{ knp_menu_render(breadCrumbItem, {'template': 'CoreBundle:Menu:breadcrumb.html.twig'}) }}

Rendering

    {% block root %}
        <ul class="breadcrumb">
            {% for item in item.breadcrumbsArray %}
                {%- if loop.last == false -%}
                    <li>{%- if item.uri -%}<a
                        href="{{ item.uri }}">{{ item.label|trans({}, 'menu') }}</a>{%- else -%}{{ item.label|trans({}, 'menu') }}{%- endif -%}
                        &nbsp;<span class="divider">/</span></li>
                {% else %}
                    <li class="active">{{ item.label|trans({}, 'menu') }}</li>
                {%- endif -%}
            {% endfor %}
        </ul>
    {% endblock %}

So since I should use now the MenuManipulator I cannot access the breadcrumbsArray anymore directly in the template. What is the intended best way to get this issue resolved? I am still willing to discuss the best way to handle breadcrumbs and the partial route matching to
add this to the documentation since those were the difficulties for me in the past.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions