1

I am working on a custom Drupal 8 module which generates a menu. Refer Create a new Menu in Drupal 8 programmatically for details.

I have the menu now, but when I try to add custom menu links to it, following add a menu link, somehow it's not getting added to that custom menu.

Another weird thing is that the links are getting added to the wrong menu, i.e. they show under User Account Management menu.

The only reason I can think of is, when I install the module, Drupal tries to create the menu link first, before it has created custom menu. As the custom menu is not there yet, it tries to add the menu links to another menu. Is that right?

Here is my code, if that helps:

// system.menu.aeauth.yml // This creates the new menu on module install

langcode: en
status: true
dependencies: {  }
id: aeauth
label: 'AE Authentication Links'
description: 'Links related to AE Authentication'
locked: true

// ae.links.menu.yml // This adds menu links to menus

ae.admin_settings:
  route_name: ae.adminForm
  title: 'AE Configuration'
  description: 'Configure AE for your website'
  parent: system.admin_config_system

ae.register:
  title: 'Register'
  description: 'Register using AE Connect'
  parent: aeauth
  route_name: ae.regroute

ae.login:
  title: 'Login'
  description: 'Login using AE Connect'
  parent: aeauth
  route_name: ae.loginroute
0

1 Answer 1

0

The problem is that you're not specifying what menu your links belong to. Instead you are setting the parent, but I think the parent doesn't exist in your cast. This works for me:

custom_module/system.menu.my-custom-menu.yml:

langcode: en
status: true
dependencies: {  }
id: my-custom-menu
label: 'My Custom Menu'
description: ''
locked: true

custom_module/config/install/custom_module.links.menu.yml:

custom_module.custom_link:
  title: 'My Custom Link'
  weight: -10
  route_name: custom_module.custom_route
  menu_name: 'my-custom-menu'

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.