21

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"]

43

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

  • 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" ] – Mohamed Osama Gbril Feb 22 '17 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 '17 at 12:16
  • The "is-active" class bug has been reported here: drupal.org/project/drupal/issues/2838351 – FiNeX Apr 18 '18 at 10:46
  • It worked for me. Thanks for such a knowledgeable solution. – Pupil Feb 27 at 5:33
2

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.

  • 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. – Juanmi May 18 '18 at 12:57
1

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;
0

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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.