4

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?

4
  • 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
    Commented Apr 14, 2015 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
    Commented Apr 14, 2015 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
    Commented Apr 15, 2015 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
    Commented Apr 15, 2015 at 11:41

3 Answers 3

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
  • 1
    This solved my problem
    – Kristoffer
    Commented Apr 7, 2016 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
5
  • 1
    tried that, unfortunately it's not giving the expected result. Updated my question now relating to this.
    – sleidig
    Commented Apr 15, 2015 at 8:29
  • 1
    @Cibes you must Clear caches after change this. Do you did it?
    – rpayanm
    Commented Apr 15, 2015 at 12:42
  • 1
    Yes, did clear caches. Changes (e.g. title) are reflected but the item is added to the "Tools" menu.
    – sleidig
    Commented Apr 15, 2015 at 14:27
  • 1
    Is weird because i test this and work for me. What is your version of Drupal 8?
    – rpayanm
    Commented Apr 15, 2015 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
    Commented Apr 15, 2015 at 15:08
2

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.

1
  • Currently, If I want to edit a menu, I have to edit each menu item separately by clicking the "edit" button. This is not user friendly. Is it possible that all menu items can be edited at once. There is a D7 module available, but nothing on D8. Does no budddy want this feature? Isn't it irritating. Here is the module. drupal.org/proj... I guess such small things are the reason most of the people still prefer wordpress.
    – Umair
    Commented Apr 1, 2021 at 10:47

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.