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

[WIP] [Outdated] Added a first implementation of a BreadcrumbRenderer #45

Closed
wants to merge 3 commits into from

Conversation

stof
Copy link
Collaborator

@stof stof commented Apr 17, 2012

This is a WIP about #24 meant for discussion.

This implementation uses the getBreadcrumbsArray method, making it easy to append additional stuff at the end of the breadcrumb. However, it means that you are limited when rendering each item as you only have the label and the uri. The other solution would be to build an array of items or to call getParent each time, but it would make it harder to pass additional paths as it would require building items for them (in this regards, an array of items would be easier than calling getParent during the rendering though as we don't need to add the new items in the existing menu tree).

To use it, you need to call the knp_menu_render method with the item corresponding to the end of the breadcrumbs, i.e. the item for which the breadcrumb is rendered (except the additional path of course). This is a good use case to get an item by path in the tree

@stof
Copy link
Collaborator Author

stof commented Apr 17, 2012

@dbu @uwej711 could you take a look at this ?


namespace Knp\Menu\Renderer;

use \Knp\Menu\ItemInterface;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First \ is not required.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed. I missed it because I created the file by copying the ListRenderer

@cordoval
Copy link

does this PR has tests?

@stof
Copy link
Collaborator Author

stof commented Apr 17, 2012

@cordoval as you can see in the diff, there is no tests at all. But I started the description by saying it is a WIP meant for discussion with the CMF guys asking for the feature (and any other guy interested in it)

break;
}

return str_repeat(' ', $spacing).$html."\n";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just about readability of the html source code, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed. This is used to render some nice HTML (which can be changed by setting the compressed option to true, like other renderers)

@dbu
Copy link
Collaborator

dbu commented Apr 18, 2012

thanks for taking this up! i think its on a good track.

from drupal i remember that you had the option to tell the breadcrumb to not show the current item, or to not link but make it just text. not showing the current menu item would here mean to just render the parent of the current item when calling the twig function, right? should we maybe add an option whether the current item should be linked or not? and anyway add a special class to it so it can be stiled differently?

@stof
Copy link
Collaborator Author

stof commented Apr 18, 2012

Regarding not rendering the current item, it is indeed as easy as passing the parent to the render call.

About adding a special class for the current item, it is a bit hard as we only have the label and the uri currently. So the first point would be discussing if we use getBreadcrumbsArray or if we find a way to turn the additional elements into items too to keep more details.

@dbu
Copy link
Collaborator

dbu commented Apr 20, 2012

RE special class for current item: you are right. i think we should rather take this and recommend to extend the renderer for special requirements like this.

one question remains: could we use twig templates for the rendering or do you really want to render all in php? (see comment on the code where its looking rather complicated)

but i have no experience with twig extension best practice, maybe the overhead is too big? i don't have a strong opinion but if its doable twig could be nice (also to extend by just replacing a twig template rather than extending the renderer class).

@stof
Copy link
Collaborator Author

stof commented Apr 20, 2012

@dbu My only issue with Twig is the way default options are handled. The BreadcrumbRenderer uses different options, and I would like to avoid duplicating the TwigRenderer for this. So I need to find a better way to handle this (which is why I started with a whole separate Renderer).

What do you think about my point about using item vs using arrays ? Btw, there is an issue with the breadcrumbs array (see the ticket linked above)

@dbu
Copy link
Collaborator

dbu commented Apr 20, 2012

sounds to me like we should do a bc break for #47 anyway, its a rather surprising and unnecessary constraint to not be allowed the same label twice.

we could define the new structure as array of array (the main array being the breadcrumbs, with insignificant indexes) and the inner array containing fields 'label' and 'url' (or whatever we previously had) and an 'item' that is the item object if there is any, in case you have a custom renderer that does more stuff with it. wdyt?

@dbu
Copy link
Collaborator

dbu commented May 22, 2012

cool, what is missing to merge this?

@stof
Copy link
Collaborator Author

stof commented May 22, 2012

I think it is quite ready. I will look at it this evening. Then, I will need to check how it could be done using Twig without duplicating the renderer

@havvg
Copy link
Contributor

havvg commented May 24, 2012

I just tried it, and it currently renders nodes that should not be displayed. I applied this patch to get it working.

diff --git a/src/Knp/Menu/Renderer/BreadcrumbRenderer.php b/src/Knp/Menu/Renderer/BreadcrumbRenderer.php
index a7aab60..5d7492b 100644
--- a/src/Knp/Menu/Renderer/BreadcrumbRenderer.php
+++ b/src/Knp/Menu/Renderer/BreadcrumbRenderer.php
@@ -92,6 +92,10 @@ class BreadcrumbRenderer extends Renderer implements RendererInterface
      */
     protected function renderItem($label, $uri, array $options, ItemInterface $item = null)
     {
+        if (null !== $item && !$item->isDisplayed()) {
+            return '';
+        }
+
         $isCurrent = null !== $item && $item->isCurrent();
         $attributes = $isCurrent ? array('class' => $options['current_class']) : array();

@stof
Copy link
Collaborator Author

stof commented May 24, 2012

@havvg when an item is not displayed in a menu, none of its children are displayed either. Your change would not do it. And btw, this would mean that you are explicitly asking to display the breadcrumb for a hidden item, so I'm not sure we should hide it in such case.

@havvg
Copy link
Contributor

havvg commented May 24, 2012

Hm.. then I'm doing it wrong, I guess. I'm retrieving it like this: (Symfony2 + KnpMenuBundle)

This renders the root item + the menu (patch reverted):

{{ knp_menu_render(knp_menu_get('main', ['profile', 'show']), {}, 'breadcrumb') }}

This code does not (default twig renderer), it only renders the children of the root item:

{{ knp_menu_render('main') }}

Did I miss something?

@stof
Copy link
Collaborator Author

stof commented May 24, 2012

@havvg I know that the current code shows every node in the breadcrumb. But try to hide the profile node in your example. With your patch, show would still be displayed whereas it does not appear in the full menu either (as its parent is hidden). And your breadcrumb code is explicitly asking to display the breadcrumb for the main / profile / show item. So I would also find it weird to hide it.

@havvg
Copy link
Contributor

havvg commented May 24, 2012

Seems reasonable; then I'm asking, why the second call is not rendering the root node, but only its children and how may I accomplish this with the breadcrumb, too?

@stof
Copy link
Collaborator Author

stof commented May 24, 2012

ah yeah, the breadcrumb array includes the root item. This should probably be changed. This methods needs more changes...

{
$this->defaultOptions = array_merge(array(
'current_as_link' => true,
'current_class' => 'current',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intended to have those options with underscore? The other renderers have their options called currentAsLink, currentClass.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only place where the library does not follow the Symfony2 standards is for array keys, where underscores should be used (which also leads to a violation of the Twig CS for the same reason). So I'm wondering about changing them to underscores in the v2 of the library, but I'm not sure yet (it would be a BC break)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho it should be compatible with the current renderers and break BC with all at once at some version.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the master branch of the library is the dev branch for 2.0. I want to think a bit more about this renaming, and about the issues related to the breadcrumb (see other comments) but such renaming would be done not long after the merge. But I will open a separate issue to discuss it

@TomiS
Copy link

TomiS commented May 24, 2012

Hey, this seems to be an interesting effort. Maybe I could get involved somehow too... I finally got it loading after I realized I have to define a service that provides breadcrumb renderer (like this)

services:
    knp_menu.renderer.breadcrumb:
        class: Knp\Menu\Renderer\BreadcrumbRenderer
        tags:
            - { name: knp_menu.renderer, alias: breadcrumb }

Now I'm wondering why it only renders the root node if I leave all extra parameters out. But anyway, it's 1.30 am, so I'm gonna have to look at this more tomorrow.

@stof
Copy link
Collaborator Author

stof commented May 24, 2012

As described above, you need to call the render method by passing it the item for which you want to render the breadcrumb, not the root.

@TomiS
Copy link

TomiS commented May 24, 2012

@stof Okay, so is there a generic way of retrieving the item for current url/route (from twig point of view)? I guess the path method isn't something that can generally be used because path variables are not 'known' in any random bundle/template that uses the menu.

@dbu
Copy link
Collaborator

dbu commented May 30, 2012

rendering the root node would best be optional. some people want it, others don't.

@TomiS
Copy link

TomiS commented May 30, 2012

Yep, I checked that I'd be able to do what I need to do with css selector :last-child. But it's IE9+ only. So not a true option for any real application quite yet. I'm not really sure how this should be solved. I'm just saying, the 'last' class for the last item (how ever it is solved) might be needed.

@Mopster
Copy link

Mopster commented Oct 11, 2012

Any new developments in this PR ? I'd like to use this breadcrumb renderer.

@tiagojsag
Copy link

+1 for this

@maxailloud
Copy link

Any news on this PR?
I'm searching a good way to make a breadcrumb.

@davidgorges
Copy link

+1

1 similar comment
@jmontoyaa
Copy link
Contributor

+1

@mikemeier
Copy link

+1

5 similar comments
@felipep
Copy link

felipep commented Jun 12, 2013

+1

@dominikzogg
Copy link

+1

@ahilles107
Copy link

👍

@bitsche
Copy link

bitsche commented Jun 28, 2013

+1

@artworkad
Copy link

+1

@hugohenrique
Copy link

this implementation of the breadcrumb was in what state?

@artworkad
Copy link

@thvd
Copy link

thvd commented Jan 17, 2014

+1

@jdespatis
Copy link

Any news on this feature?

@Nek-
Copy link
Contributor

Nek- commented Feb 28, 2014

@stof nothing against rebasing on master this branch ?

@stof
Copy link
Collaborator Author

stof commented Feb 28, 2014

@Nek- Actually, I'm not convinced this is the appropriate approach:

  • normal renderer requires passing the root menu
  • this breadcrumb renderer requires passing the leaf menu

this could make it really confusing to use. However, I have no real idea about a good implementation.

thus, we don't have yet a way to get the current item easily from Twig (we have a way to get it in PHP thanks to the filter iterator)

@Nek-
Copy link
Contributor

Nek- commented Feb 28, 2014

@stof can you come on gitter ( https://gitter.im/KnpLabs/KnpMenu ) to speak about the future of KnpMenu ?

@stof
Copy link
Collaborator Author

stof commented Feb 28, 2014

not really during work time. Is it OK for you if we do it this evening or during the weekend ?

@Nek-
Copy link
Contributor

Nek- commented Feb 28, 2014

It will be perfect at the evening, thanks :-) .

@Nek- Nek- removed this from the 2.0 beta milestone Mar 17, 2014
@pestaa
Copy link

pestaa commented May 6, 2014

+1

@Nek- Nek- changed the title Added a first implementation of a BreadcrumbRenderer [WIP] [Outdated] Added a first implementation of a BreadcrumbRenderer May 15, 2014
@Nek- Nek- closed this May 26, 2014
@lsmith77
Copy link
Collaborator

i guess #161 aims to address this for 2.1?

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

Successfully merging this pull request may close these issues.