23

In a Drupal 8 site, I want to add menu items with "no link", I was thinking that nolink is allowed but when I entered this, I got error

Manually entered paths should start with /, ? or #.

Is there a way for adding menu items not clickable without adding "#" because "#" add attribute [data-drupal-link-system-path=""] and [class="is-active"]

4 Answers 4

56

You need to use route:<nolink> to achieve this. Then it will validate and render without a link.

7
  • Thanks so much, I tried it and it working fine, but still adding attributes of [ data-drupal-link-system-path="<front>" ] and [ class="is-active" ] Feb 22, 2017 at 12:13
  • 1
    That is a separate issue from what you're trying to achieve and either a core bug or problem with your theme, but route:<nolink> is definitely the answer. It even works with Pathauto.
    – Kevin
    Feb 22, 2017 at 12:16
  • The "is-active" class bug has been reported here: drupal.org/project/drupal/issues/2838351
    – FiNeX
    Apr 18, 2018 at 10:46
  • It worked for me. Thanks for such a knowledgeable solution.
    – Pupil
    Feb 27, 2019 at 5:33
  • +1 upvote - thank you - <nolink> works when adding link in standard Drupal menu, admin/structure/menu/manage/main/add so that you can have other words on menu, perhaps to go before a set of links that have similar purpose, e.g. social links (using menu_link_attributes & fontawesome_menu_icons modules. Then also add nth child css and the menu can be positioned separately from other links. menu_export is handy for putting menu items into config. Feb 18, 2021 at 20:26
5

To create a menu item without link in a custom module add to [module_name].links.menu.yml:

url: 'internal:##'

You need to add a second # character (or any other letter or word url: 'internal:#nolink') in order to the href attribute will not rendered empty, which will cause a page reload if the user click the link.

1
  • 1
    This is a good solution also. For me better than remove the link, that removes the <a> tag and then this menu item loose the corresponding css styles. May 18, 2018 at 12:57
2

and add class or style for item [a style="pointer-events:none; cursor:default; text-decoration:none;" ...][/a]

pointer-events:none; cursor:default; text-decoration:none;
1

For Drupal 8 you can create a link programmatically using Link::fromTextAndUrl not linkable like that:

$my_link = Link::fromTextAndUrl(t('This is my link text'), Url::fromUri('route:<nolink>'));

Optionally you can convert it to an array using: $my_link = $my_link->toRenderable();

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.