Is it possible to create a new Menu in Drupal 8 programmatically using a custom module?

I am creating a custom module, and one of the requirements of the module is that it creates a new menu under Structure > Menus.

Most of the tutorials teach how to add a menu link to an existing menu. For example, Creating menu links, Add a menu link, etc.

In short, I need a way so that the end result is a menu below "User Account Menu" in the image, and then I can add my menu links to it, using above tutorials.


enter image description here

  • Click the blue button in the image. “Add Menu”.
    – Kevin
    Dec 7, 2017 at 0:48
  • Sorry, forgot to mention that I want to do it programmatically, using my custom module. Dec 7, 2017 at 0:51

2 Answers 2


Menus are configuration; you can include one in your module by adding a file named <module_root>/config/install/system.menu.foo.yml to your codebase (replacing foo with the desired menu ID).

The contents of the file will be something similar to one of the same configs installed by the core system module, e.g.

langcode: en
status: true
dependencies: {  }
id: foo
label: 'Foo menu'
description: 'Links related to foo'
locked: true

When the module is (re)installed, the menu will be created.

You could also technically use the API in a hook_install or similar:

$menu = \Drupal::entityTypeManager()->getStorage('menu')
    'langcode' => 'en',
    'status' => TRUE,

but the yml method is a bit cleaner, and I think recommended (can't find a source right now though).

As an aside, the menus are listed in alphabetical order. I don't think it would be straightforward to change that, menus have no concept of weight in relation to one another so you'd have to wedge that in somehow if you really need your menu at the bottom of the list.

  • Thank you for the detailed answer. I was able to add a new menu successfully. I don't really need to place it at the bottom, that was just to illustrate my need to add a new menu to the current list of menus. Dec 7, 2017 at 17:46
  • Just wondering, where is this information in the documentation? How were you able to figure it out the first time? I am new to Drupal development and having a hard time figuring out where to search for many other issues similar to this. Dec 7, 2017 at 17:48
  • 4
    Difficult to say - I followed Drupal 8’s development really closely so I just kind of know where things are these days. Plus I used Drupal 7 for years so it was more a matter of transposing concepts from one version to the other than figuring it all out from scratch. In my experience learning by doing is the only real way to learn Drupal. That and the examples module, the change records, the issue queues, and this site
    – Clive
    Dec 7, 2017 at 17:54

Instead of re-installing the module you can import the newly created configuration via drush command.

drush config:import --source="modules/custom/[MODULE_NAME]/config/install/" --partial

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.