4

When I visit a page , say 'About Us' I want the 'About Us' in the menu-items to act as a simple text (not as a link). How can I achieve that ? Thank you in advance.

5 Answers 5

2

You can use the Special menu items module.
In your case you can use the <nolink> placeholder in the menu link path field to show the menu item as text.

1
  • Thank you. But I want the menu item as text only when the user is visiting the corresponding page. Using <nolink> placeholder makes it a text everywhere. Jul 10, 2012 at 10:53
1

You can do this in your theme template with YOURTHEME_menu_link().

You need to replace YOURTHEME with whatever your theme is called.

function YOURTHEME_menu_link(array $variables) {

$element = $variables['element'];
$sub_menu = '';
$name_id = strtolower(strip_tags($element['#title']));
if (strpos($name_id, ':')) $name_id = substr ($name_id, 0, strpos($name_id, ':'));
$pattern = '/[^a-z]+/ ';
$name_id = preg_replace($pattern, '', $name_id); //this is what the link is called
$element['#attributes']['class'][] = $name_id;

//you can check the values of your links here by printing $name_id

if ($name_id == 'About Us') { //this is just an example, if it were called this it would probably be in lowercase
    $output = $element['#title']; //this will just output the title of what was going to be a link
} else { //else create links for the menu
    $output = l($element['#title'], $element['#href'], $element['#localized_options']);
}

return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n"; //this just puts it all together

}

If your using a contributed theme its most likely you will have something like this in your theme template already so its just a case of making some amendments.

BE SURE TO CLEAR CACHES ;-)

4
  • Thank you . I tried using this code but didnot work. May be because I am not able to understand the code fully and change it accuording to my requirement. I would be really grateful if you could write comments for some part of the code that should be changed. Jul 10, 2012 at 10:51
  • Comments added ;-)
    – Alex Gill
    Jul 10, 2012 at 10:56
  • Thanx a lot I will try this again and let you know if it works. Jul 10, 2012 at 11:03
  • you are not returning the "variables", I'm wondering when you have submenu if they will still be displayed.
    – GwenM
    Dec 4, 2013 at 0:59
0

Most likely, the link for the page your are currently visiting will be marked as visited :

<a href="/about-us" class="active" >About us</a>

So you can use this attribute to add some jQuery disabling the link

$("a.active").replaceWith($("a.active").html());

JS Bin

0

By default, all menu items are "enabled." This means they display. Menu items can also be disabled though. A disabled menu item knows who its parent is, so the related sub-menu and breadcrumb will display. But - you don't have to necessarily clutter up your menu with another visible link. (Note: this is only really applicable to the main menu since that's what drives the sub-menu and breadcrumb.)

To create a disabled menu item:

  1. Put the page/webform/list into the menu using the menu settings section of that content's edit form.
  2. Click "Main menu" in the gray shortcut bar.
  3. Find the menu item you want to disable.
  4. Uncheck "enabled" next to it.
  5. Click save at the bottom.

If you want to create a sub-menu below a page that doesn't display in the top menu, just disable the top menu item and leave it's children enabled.

0

Updated answer from: Non-Clickable Menu Items by @Guest312312

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.

Example:

# [module_name].links.menu.yml
edesk_toolbar.admin_help:
  title: My link name here
  url: 'internal:##'
  description: 'A nolink menu item'
  parent: system.admin
  menu_name: admin
  weight: 999

The above eaxmple is tested and works in drupal 10.1

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.