0

I've been finding a lot of info about how to add a menu link as part of a module. That works fine using the route/controller & links.menu.yml file.

But I am now trying to figure out how to add a complete main menu for my module (my module is a default data & structure module).

My goal is to have x level-1 menu items and each one of those will have varying numbers of sub-menus.

Any hints or references to guides/tutorials?

2
  • If you found answers in the documentation for part of your question, please link to said documentation in your question. Doing so helps other people who find your question and are not as far along as you. Commented Jul 26, 2020 at 6:38
  • I abandoned this method in favor of using a completely different technique. The technique that works for me is using the "Default Content" Drupal module which lets you export content from a dev system and package that content up as a module. When the module is enabled on a new system the previously exported content is ingested.
    – HeneryH
    Commented Jul 27, 2020 at 0:41

2 Answers 2

0

You can multiple links in the same way that you add one link.

This creates a front page link on a menu for anonymous users and a front page link on a menu for authenticated users, as well as a parent and child menu link (Fruits -> Apple) for anonymous users.

MYMODULE.links.yml:

MYMODULE.anon-home:
  title: 'Home'
  route_name: 'entity.node.canonical'
  route_parameters: { node: '1' }
  weight: 0
  menu_name: main-navigation-anon
MYMODULE.anon-fruits:
  title: 'Fruits'
  route_name: 'entity.node.canonical'
  route_parameters: { node: '4' }
  weight: 10
  menu_name: main-navigation-anon-ja
MYMODULE.anon-apples:
  title: 'Apples'
  route_name: 'entity.node.canonical'
  route_parameters: { node: '6' }
  parent: MYMODULE.anon-fruits
  weight: 0
  menu_name: main-navigation-anon-ja
MYMODULE.auth-home:
  title: 'Home'
  route_name: 'entity.node.canonical'
  route_parameters: { node: '1' }
  weight: 0
  menu_name: main-navigation-auth
1
  • Thank you Patrick for this. I was not sure how to tackle the parent/child aspect of this. For now I am switching to the Default Content module as a way to import but may have to come back to this if that method does not pan out.
    – HeneryH
    Commented Jul 27, 2020 at 0:42
0

I've decided to go with the default_content technique in which it is possible to save off the menu items to a 'custom_module' then reload them into a new instance.

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.