3

With my module MYMODULE I would like to create external link and place it into main menu. Is it possible to do that in MYMODULE.links.menu.yml file or MYMODULE.routing.yml?

Something like this???

mymodule.links.menu.yml

mymodule.googlecom:
  title: 'Google'
  route_name: 'googlecom'
  menu_name: main

mymodule.routing.yml

googlecom:
  path: 'http://google.com/'

2 Answers 2

6
mymodule.menu_external_link:
  title: 'External link'
  url: 'https://www.drupal.org'
  menu_name: main
  weight: 10
  options:
    attributes:
      target: _blank
      class: my-class
0
4

routing.yml are routes of your site, so they can not be external.

links.menu.yml are links that point to routes, so they can not be external.

To create external links, you need to create them as a menu link content entity in your hook_install() function.

Something like this:

  $menu_link = MenuLinkContent::create(array(
      'title' => 'Link title',
      'link' => ['uri' => 'https://www.example.com'],
      'menu_name' => 'navigation',
      'weight' => 0,
      'external' => TRUE,
      'bundle' => 'menu_link_content',
    ));
  $menu_link->save();

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.