1

I want to create a new menu in Drupal 8 when I install my module. How can I achieve this?

I don't want to add a link to an existing menu, but create a new one.

7

You can either include the menu in your modules default configuration by adding a file to config/install folder of the module:

system.menu.menu-name.yml:

langcode: en
status: true
dependencies: {  }
id: menu-name
label: 'Menu label'
description: 'Menu description'
locked: false

You can also create the Menu entity, perhaps in the hook_install() hook.

use Drupal\system\Entity\Menu;

function MY_MODULE_install() {
  $menu = Menu::create([
    'id' => 'menu_id',
    'label' => 'Menu label',
  ])->save();
}
0

Menus are just entities in Drupal 8. Just create it with:

\Drupal\menu\Entity\Menu::create([
  'id' => 'my_menu',
  'label' => 'My Menu',
  'description' => 'This is my menu!',
]);

Use this in the hook_install() so the menu is created in the installation process.

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.