3

For my custom Drupal 8 module I want to add a link to the default main navigation menu of the site.

What parent ID do I have to give to insert my menu item into the main menu?

I currently have the following config in my_module.links.menu.yml:

entity.sss_topic.collection:
  title: 'Topic: Listing'
  route_name: entity.sss_topic.collection
  description: 'List Topic'
  parent: main

But that only adds a menu item into the "Tools" menu (the same effect as specifying no parent whatsoever). However, parent: system.admin_structure does work correctly.

Do I have the wrong parent ID? Or maybe a bug?

  • Have you read the docs? They go into a good amount of detail, perhaps you could edit the question to show where you're stuck? – Clive Apr 14 '15 at 12:14
  • Mainly I am looking for the right ID for the parent parameter in order to insert into the main navigation menu. – sleidig Apr 14 '15 at 12:33
  • parent is not the menu, it is the parent menu link. Just leave parent out, main should be the default menu anyway. – Berdir Apr 15 '15 at 9:13
  • Thanks, that's good to know. Seems like "Tools" is the default, however. Do you know how I can insert into a different menu? – sleidig Apr 15 '15 at 11:41
6

You are missing the "menu_name" key as said on : https://www.drupal.org/node/2122241

this example should work :

demo.demo:
  title: Demo Link
  description: 'This is a demo link'
  parent: main
  route_name: demo.demo
  menu_name: main
  • 1
    This solved my problem – Kristoffer Apr 7 '16 at 13:47
2

Beware: Build a Drupal 8 Module: Routing, Controllers and Menu Links is outdate, for example on demo.routing.yml is no longer _content, now is _controller.

Relate to add a menu item to the main navigation you can try with the parent main, like this:

demo.demo:
  title: Demo Link
  description: 'This is a demo link'
  parent: main
  route_name: demo.demo
  • 1
    tried that, unfortunately it's not giving the expected result. Updated my question now relating to this. – sleidig Apr 15 '15 at 8:29
  • 1
    @Cibes you must Clear caches after change this. Do you did it? – rpayanm Apr 15 '15 at 12:42
  • 1
    Yes, did clear caches. Changes (e.g. title) are reflected but the item is added to the "Tools" menu. – sleidig Apr 15 '15 at 14:27
  • 1
    Is weird because i test this and work for me. What is your version of Drupal 8? – rpayanm Apr 15 '15 at 15:07
  • 1
    using the beta9. I am now using the workaround of displaying the "Tools" menu in place of the "Main Navigation" menu on my site ... – sleidig Apr 15 '15 at 15:08
1

I can confirm when set the "parent" property to be "main" in menu.link.yml will get your menu item display below in the default "Tools" as a submenu which is pretty weird. Anyone know how to make it happen to set up a menu item in the main menu?

If anyone come to this page just for add a simple menu item to the main menu list which created by a module configured by module.links.menu.yml.

Like I have a catalogue module which need to add a menu item titled "catalogue" to the main menu so I can get to that page quickly.

# add below code to catalogue.links.menu.yml
catalogue.page:
  title: 'Catalogue'
  description: 'some description about the catalogue menu item'
  route_name: catalogue.page
  weight: -20
  menu_name: main
  options:
    attributes:
      target: _blank

Note the "menu_name" defines which menu should the item be put in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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