I want to show main menu links in the sidebar. I have placed that menu block in sidebar region from admin side.

But based on the current URL, i need to display that particular menu item.

Item1 (item1)
  Item1_Sub1_item1 (item1/sublev1)
     Item1_Sub1_Sub2_item1 (item1/sublev1/subsublev1)
     Item1_Sub1_Sub2_item2 (item1/sublev1/subsublev2)
Item 2
  1. If the current url is / (home), then my menu should diplay all menu item

  2. If the current URL (item1/sublev1) match to menu item (Item1_Sub1_item1) then only the parent and child of that menu should be displayed as below

Item1 (item1)
  Item1_Sub1_item1 (item1/sublev1)
     Item1_Sub1_Sub2_item1 (item1/sublev1/subsublev1)
     Item1_Sub1_Sub2_item2 (item1/sublev1/subsublev2)
  1. If the current URL (item1/sublev1/subsublev1) match to menu item (Item1_Sub1_Sub2_item1) then only the parent and child of that menu should be displayed as below
Item1_Sub1_item1 (item1/sublev1)
  Item1_Sub1_Sub2_item1 (item1/sublev1/subsublev1)

I have overridden the menu--main.html.twig file using theme suggestion and using menu--main-sidebar.html.twig file to design the menu.

Any idea how to achieve this? My guess is,

  1. I need to create a block programmatically to do all these operations
  2. Display that block in my html.twig.file

If I am right, how to do that in theme? If i am wrong, what else need to do?

For info I am already using Menu_block module, but I can't place block for each URL. So i want to program it.

1 Answer 1


Yeah, I think your guess is good.

  1. Create a block programmatically: Create a custom Block. This means to create an own module with this block.
  2. The created block should appear (when the module is activated) in your block settings, there you can add it.
  3. The content of your block appears where you added it.

Another option, easier but not so beautiful, could be to hook in your THEME.theme. As an example you can add the menu in the template_preprocess_html:

function template_preprocess_html(&$variables) {
    $variables['my_own_menu'] = $my_own_menu;

Then you can use the variable {{ my_own_menu }} in your html.html.twig and do whatever you want to do.

  • Thanks. But, block can not be created within theme itself?
    – siddiq
    Dec 11, 2017 at 7:43
  • AFAIK not. In the preprocess, you cannot add new functionality, but you can add elements, change things and some other stuff. So you can add your complete menu if you want, with all it's parts. If you want to have the menu separated from your theme, you need to build the block / own module.
    – btemperli
    Dec 11, 2017 at 7:55
  • If you want to go with the variant theme / preprocess, i suggest to separate the menu anyway: THEME_preprocess_html: $variables['my_own_menu'] = get_my_menu_content(); html.html.twig: {% include '@THEME/your-menu-twig-file.html.twig %}
    – btemperli
    Dec 11, 2017 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.