David Lohmeyer's Blog

Nesting module-defined menu links in Drupal 8

When you provide a module-defined menu link in Drupal 8, there is some great documentation on how to add a menu link on Drupal.org. This gets into how to provide a menu link with YAML. In a lot of cases, you might want to nest this menu link under another item. This is especially the case if you were providing a menu link for something in the main menu of your site.

The 'parent' indicator in the my_module.links.menu.yml file is how you nest items. However, you need to find the menu link item's "id", which is actually terribly confusing because the id is usually found by hovering over your menu edit link on the menu structure page and pulling out a number like "22", or whatever your menu item id is in your browser. The menu will show your item on the top level of whatever menu you are targeting if it can't find that parent id. And it won't, because it's not obvious how to find it...

Hold on, though! Here's where your Drupal brain is going to explode, because there is some sort of internal menu link id that is used in the database. One is not able to find this id unless they look in the menu_tree table in Drupal. One way to find it in this table is if you search for a sibling menu item in the "route_param_key" column by its node id (example: node=250074) you should be able to find the "parent" column of that one. There will be other ways to search this table, but it's not obvious. Your "parent" that you put in YAML will look something like this for menu content items: menu_link_content:d37de038-32a1-4bb5-8d79-9fcbbcbeaf8d

Here's hoping the user experience of finding this menu link id becomes easier at some point!

Related Posts