0

If a menu item has a submenu and I specify the hash symbol (#) in the Link field of such parent menu item in the admin panel, I receive empty url at the frontend and therefore if click by such link, I follow to the homepage.

Even if I use in my menu--main.html.twig template the code like

{% set menu_item_url = item.url %}
{% if menu_item_url == '' %}
{% set menu_item_url = '#' %}
{% endif %}
<a href="{{ menu_item_url }}" ...

I still receive empty href=""

How to resolve the problem? I mean is there a way inside of templates? Of course, I know that I can resolve the problem with JS.

Note: I don't mean an anchor, I just need # as parent item url if the item has a submenu.

7
  • What exactly did you enter as link? /#some-id?
    – leymannx
    Sep 20, 2021 at 7:28
  • 1
    Does this answer your question? How can I have links to anchors in menus?
    – leymannx
    Sep 20, 2021 at 7:29
  • No, I don't mean an anchor. I just need # as parent item url if the item has a submenu
    – stckvrw
    Sep 20, 2021 at 10:01
  • I don't understand. You want <a href="#">? You know you can enter <nolink> in the menu UI if the menu item shouldn't be a link?
    – leymannx
    Sep 20, 2021 at 11:28
  • Maybe look at Non-Clickable Menu Items.
    – leymannx
    Sep 20, 2021 at 11:30

2 Answers 2

0

Here is an example, where clicking 'Example menulink' will not send you elsewhere is you have a submenu item. Otherwise use url: 'internal:##'

MYMODULE.menuitemxyz:
  title: 'Example menulink'
  url: 'internal:#'
  menu_name: mycustom-menu
  weight: 1009
  enabled: 1
  expanded: 1

MYMODULE.menuitesub1:
  title: 'Anchor link'
  parent: MYMODULE.menuitemxyz
  menu_name: mycustom-menu
  url: 'internal:#using_anchors'
  weight: 1007
-1

If you do not want a link in the parent menu item, you may also use <none> as link. I always use it for menu items without a link.

1
  • No that's not what they want. See the comments.
    – leymannx
    Sep 21, 2021 at 16:36

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.