1

I am desperately trying to render a submenu in symfony-cmf.

Example

Structure:

page1
  ├─p1-subpage1
  ├─p1-subpage2
  └─p1-subpage3
page2
  ├─p2-subpage1
  └─p2-subpage2

Whenever the current page is somewhere within the page1 hierarchy it should use p1-subpage* to render the menu, when I am within the page2 hierarchy it should use p2-subpage* to render the menu. Technically that means it should set the current item to the parent of the 1st level (if it's not already on it) and render one level of nodes (e.g. knp_menu_render('main', { depth: 1 })).

The problem can be split in two parts:

  1. Rendering a (sub-)menu from a given node
  2. Retrieving the current node

Thoughts and Trials

  • TWIG: It has been suggested to support rendering submenus as a functionality of the KnpMenu itself, but it hasn't been done. As a workaround registering a twig extension has been provided by someone in the issue. However this extension is based on the getCurrentItem Method which has been removed with KNP-Menu 2.0. Although the cmf currently uses v1.1 of the knp-menubundle, this is going to change soon
  • TWIG: The CnertaBreadcrumbBundle would bring back this functionality, but depends on KNP-Menu 2.0 as well.
  • TWIG: Using a hack similar as suggested here. It checks the current URI, counts the number of slashes and decides based on that what to use. This could probably work. Problem here: I don't have cmfMainContent variable defined, nor can I find anything similar in my {{ dump() }} (nothing containig a menu either).
  • RouteVoter: The cmf itself has some MenuVoters itself, which are well documented what they are, but not how to use them. I don't think that there is any way to access that functionality whithin twig nor do I know how to intercept the menu building.

Thanks for any help.

Patrick
  • 879
  • 2
  • 11
  • 29

3 Answers3

1

have a look here for an example of using voters to make decision about what to highlight: https://github.com/dbu/conference-tutorial-1.0/pull/20

aside from this we are making good progress on a KnpMenu 2.x compatible version of our MenuBundle but it might be until January until we make a stable release of it (but we might make one earlier .. we will see): https://github.com/symfony-cmf/MenuBundle/pull/214

  • Lukas: I think Patrick is hoping for an answer to https://github.com/symfony-cmf/MenuBundle/issues/57 - which i unfortunately do not know – dbu Nov 06 '14 at 07:30
  • Thanks for that. I don't quite understand what it actually does. @dbu is exactly right. Will this be easier with knpMenu 2.x? I wonder why the twig getters have been removed. I'll probably end up with a nasty js/css solution by absolutely positioning the submenu... – Patrick Nov 06 '14 at 11:16
1

I have created a bundle yesterday for my own, similiar, use case.

However as all of my pages share the same route you might need to adapt it quite a bit.

I still think you might find some inspiration, especially for the second part of your problem.

My Bundle: https://github.com/burki94/RecursiveMenuBundle/blob/master/README.md

AbstractRecursiveBuilder: https://github.com/burki94/RecursiveMenuBundle/blob/master/Menu/AbstractRecursiveBuilder.php:

Marcel Burkhard
  • 3,453
  • 1
  • 29
  • 35
  • Thanks for your input! This looks interesting, however I have no idea how to embed this in cmf as I don't build the menu myself. – Patrick Nov 06 '14 at 11:18
  • You should have {{ knp_menu_render('something') }} somewhere in your twig templates. (see: https://github.com/symfony-cmf/standard-edition/blob/1.2/src/Acme/DemoBundle/Resources/views/layout.html.twig) Just replace that. And if you get your "pages" from an Document Manager read this: http://symfony.com/doc/current/cmf/tutorial/the-frontend.html and inject the DocumentManger to your new MenuBuilder aswell as the other services. I'm not familiar with symfony-cmf so I'm not sure this will work. – Marcel Burkhard Nov 06 '14 at 12:20
  • I am not really familiar with cmf either, so I'm not quite sure how to config and fetch the pages. But it seems reasonable to me. I might going to try it again, but for the moment I found a (deprecated) solution.Thx – Patrick Nov 06 '14 at 15:39
0

This is not really a solution because it's not following my requirements of being compatible with KnpMenu 2.*. But this deprecated solution is easy:

     {% set currentItem = knp_menu_get('main').currentItem %}
     {% if currentItem is not null %}
            {% if currentItem.getLevel() == 1 %}
                  {% set main = currentItem %}
            {% else %}
                  {% set main = currentItem.getParent() %}
            {% endif %}
            {{ knp_menu_render(main, { 'template': 'ComBundle:Default:left_menu.html.twig', 'currentClass': 'uk-active' }) }}
     {% endif %}
Patrick
  • 879
  • 2
  • 11
  • 29