Profile picture for user admin
Daniel Sipos
10 Mar 2014

In the previous article I showed you how you can add menu items with wildcards to your menus in custom modules. Since then, however, I've been shown a cool module that allows to do similar things through the UI: Menu token.

Menu token allows you to define menu items that include tokens (provided by the Token module). These tokens get replaced on page load depending on the context or whatever rule you set when you created the menu. So let's take a look at how it works.

To install it, go through the normal module installation procedure. With Drush, this is simple:

drush dl menu_token token && drush en menu_token token

You'll notice that we also install the Token module as it is needed.

Before anything, navigate to admin/config/menu_token and check/uncheck the relevant boxes. These are basically the available entity types exposed by Menu Token.

Next, go to your menu and add a new link. You'll notice a new checkbox saying Use tokens in title and in path. Check that box and you'll get all the options for the token in that menu link.

Example #1: Current User ID

Let's see how we can create a menu link that goes to the user page of the currently logged in user.

  1. Under Method for users, select User from context.
  2. Best to also check the box Remove token if replacement is not present to avoid problems if the token is not available on a given page
  3. Browse the available tokens and look for this one: [current-user:uid]. This is the token for the currently logged in user account ID.
  4. Add your menu title and for the path specify: user/[current-user:uid].
  5. Save

Now if you go somewhere your menu is visible, you'll notice a new link that leads to your user account page. Neat. We have't done anything that isn't already available in Drupal core, but still neat. The reason I'm saying this is that you can create a menu link with the path user/ that will lead the same place. But for illustrating the power of Menu Token, it's a good example.

Example #2: Current Node Type

Let's say you have a View that displays nodes of two different content types: Article and Basic Page. This View has a contextual filter to show nodes of the type article when the first parameter after the path to the view is article. And same for the Basic Page (with page being the machine name). The view path is nodes.

Let's create a menu link for the main menu that will link to this view and pass to the URL the content type of the currently viewed node.

  1. Under Method for nodes, select Node from context.
  2. Best to also check the box Remove token if replacement is not present to avoid problems if the token is not available on a given page
  3. Browse the available tokens and look for this one: [node:content-type:machine-name]. This is the token for the currently loaded node content type.
  4. Add you menu title and the following path: nodes/[node:content-type:machine-name]
  5. Save

Now if you navigate to an article page, you'll notice in your main menu a new link to the following path: nodes/article. If you go to a Basic page, it'll be nodes/page. And clicking on these will of course take you to your View page with the filtering applied.

Conclusion

These are a couple of simple examples of how you might use Menu Token. Of course you can use it in other situations depending on what you need and what tokens are available.

Hope this helps.

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.

Contact us

Comments

develCuy 11 Mar 2014 06:55

Thanks!

Your post is great documentation!

Don't you mind to contribute to the handbook https://drupal.org/node/1811534?

Feel free to improve and recommend improvements in the issue queue as well.

Jon 11 Jun 2014 06:11

In reply to by develCuy (not verified)

Menu Token

Useful information on Menu Token with current cser id exemple.

Capi Etheriel 22 Mar 2014 07:18

Download and enable with just one command

drush en -y menu_token will download menu_token, notice it needs token, download token and enable both modules. Type less, save your fingers for videogaming!

Hendrik 19 Apr 2014 22:18

Awesome

Thanks for this blog post! It helped me so much! I've been looking for this functionality for about 3 years and finally I could use it on my clients' website.

I don't know much of preprocesses etc so I could never accomplish this before :)

It works perfect for my drupal commerce websites that uses user/[current-user:uid]/licensed-files

Lin 22 Dec 2015 22:10

does not work!

this module (menu token) is still buggy and does not work. It logs a lot of error messages and the patches available do not provide a proper fix for the issues. IMO, it's better to wait for a good version to be released.

Samedi Amba 28 Jul 2017 11:13

In case this does not work

Thanks @Danny for the great tutorial.
However, there are cases where this module doesn't work. I installed and followed the procedure given. Nothing. Then I discovered that the inbuilt tokens work just as well.
For example, company-guests/[current-user:field-user-company:tid]/[current-user:uid] returns the current users company ID and user ID for the currently logged in user, values that I have successfully passed to the view in question, and selectively displayed content.

Hope this helps someone. Cheers...

Add new comment