How to make a Drupal menu item link to itself for use with drop down menu modules.

A problem that I have always ran across when deploying Drupal sites is that there is no "out of the box" functionality that allows you to create a menu item that doesn't link.

A scenario to describe this problem is if you are using a menu module such as Superfish or Nice Menus. Lets say you have a top parent menu item, "Business Services", as shown in the image below:

Drupal Menu Parent Link

If you want to have the parent link (Business Services) dead, so that the user can't click on it, thus forcing the user to use the drop down menu, you are out of luck. This isn't possible with the core of Drupal. The path must contain either a path to an internal Drupal node, or an external link.

It seems logical to think that Drupal would allow a menu item to link to itself since we can link to external URL's. The way I used to work around this problem was to have the parent menu item (Business Services) be a page that simply listed the drop down menu items in a bulleted list. I found this redundant and searched for a different method.

I looked into a module, Menu Item Container to allow for this functionality, and also read a blog post about hacking the core includes/menu.inc file to disable the filter that checks the path input.

The Menu Item Container module was not available for Drupal 7 yet, and I did not want to hack Drupal core since it may affect future upgrades.

Since I couldn't have a menu item that didn't link, my next approach was to try to add a token into the path field of the menu item to make it link back to itself. I tried using "[current-page:url]/#" but Drupal rejected it because Tokens are not allowed in the path of menu items. After doing more research I found a module called Menu Token which allows you to use tokens in your path.

After downloading and installing Menu Token, I then added a new menu item called "Business Services". For the path I typed "[current-page:url]/#" and checked the box that says "Use tokens in title and in path.".

Drupal Menu Item Using Tokens

I then moved the children items to be underneath the new "Business Services" menu item, and deleted the old "Business Services" node that had the bulleted lists.

Drupal Menu List Using Tokens

The problem was solved.

I feel that Drupal should allow this functionality in the core; either by adopting the Menu Token module or by modifying the includes/menu.inc file. Feel free to leave a comment with your feedback or suggestions. I would love to hear how others have worked around this issue.

About the Author

chris's picture
Chris Kirby - Owner, The Computer Group
Chris Kirby is the owner of The Computer Group and regular contributor to the blog. Chris is 31 years old and has been working with computers since the age of 6.
Wed, 07/27/2011 - 10:07pm
Average: 2.5 (52 votes)

Add comment

Comments

I too have been looking at how to do this. thanks!

this is super awesome. thanks!

That saved a bunch of time searching through modules - thanks!

Thanks a lot dear! It is really awesome. I was searching for this.

In D6 [current-page:url]/# should be [current-page-url]/# but it doesn't work. I installed Menu-Container which works great. Don't know why I tried your solution on D6 - what the heck, now that I did, here is the feedback.

Not as good as core functionality that simply supports "#" but this will do. Thanks!

This is great. I've been trying to make the special menu to work in my d7 installation but it didn't thanks to this.

This is great.

I tried this solution, but when I plug in [current-page:url]/# It always returns me with [sitename]/node/# Not the current page. Is this a bug?

I just tried this on a fresh install of Drupal 7.14 and it still worked. Have you updated all of your modules and core?

It's an up-to-date install of Drupal 7.14. There are many modules installed. You can take a look at it: http://mbsi.it-guy.net

If you want to contact me by email, I will certainly send you my contact info to figure this out.

Dave, if you want to email me the login info I can take a look for you. Email me at chris [at] thecomputergroup.com

Thanks.. problem solved...

works perfectly - just needed to apply this patch http://drupal.org/node/1899696 as well thanks chris - contributions like this is what makes working with drupal so fantastic !!!