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

BreadCrumb Array with Refactored MenuItem #194

Closed
peterrehm opened this issue Jun 26, 2013 · 11 comments
Closed

BreadCrumb Array with Refactored MenuItem #194

peterrehm opened this issue Jun 26, 2013 · 11 comments

Comments

@peterrehm
Copy link

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.

@peterrehm
Copy link
Author

@stof Can you give me a suggestion on that? Thanks in advance.

@ahilles107
Copy link

I have this same problem, wiith rendering by php it's easy - you can return from builder array with breadcrumbs:

        $manipulator = new \Knp\Menu\Util\MenuManipulator();
        return $manipulator->getBreadcrumbsArray($current);

but if you use twig it returns error - because builder function should return MenuItem object,

@stof - how we can get it work again?

@dleute
Copy link

dleute commented Jul 23, 2013

Was this ever resolved? I'm running into similar issues.

@peterrehm
Copy link
Author

Unfortunatly not, I have not got any feedback from @stof. Meanwhile I have fixed it for me by fixing the versions to

    "knplabs/knp-menu-bundle":"dev-master#f090ba5",
    "knplabs/knp-menu":"dev-master#6e3d276",

Once I get some feedback I will dig into that end eventually publish some documentation about that.

@stof
Copy link
Collaborator

stof commented Jul 23, 2013

@peterrehm Instead of using this hacky constraints (hacky because the resolution will still be done usign the metadata of master but the installer will change the hash when installing from source, while the dist installation would still install the latest master), you should require the 2.0-alpha1 version for now

@peterrehm
Copy link
Author

well yeah, makes sense. however i would love to fix the actual problem. can you support me on that as well?

@artworkad
Copy link

+1

@yakobe
Copy link

yakobe commented Oct 7, 2013

did anybody find a nice solution to this?

@peterrehm
Copy link
Author

I haven't found time to dig into the details so I disabled breadcrumbs totally for now. Once I get any feedback I will definitely have a look.

@pm-wobbuffet
Copy link

I know this is an old post, but I figured I'd throw in my solution for those having problems.
I use the MenuManipulator's getBreadCrumbsArray() function to get an array of parent items, then attach it as a an attribute to the item returned.

$mm = new MenuManipulator();
$item->setAttribute('bchistory', $mm->getBreadcrumbsArray($item, null));

Then, in the Twig template for your breadcrumb rendering:

{% block root %}
    {% if item.attributes.bchistory %}
        <ol class="breadcrumb">
        {% for  link in item.attributes.bchistory %}
    {# continue with your rendering here #}

@Nek-
Copy link
Contributor

Nek- commented Jul 4, 2014

This is more an issue of KnpMenu, please see KnpLabs/KnpMenu#161

@Nek- Nek- closed this as completed Jul 4, 2014
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

8 participants