Menu

Cool Module: Menu Token

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.

Under Method for users, select User from context.

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

Browse the available tokens and look for this one: not yet assigned. This is the token for the currently logged in user account ID.

Add your menu title and for the path specify: user/not yet assigned.

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.

Under Method for nodes, select Node from context.

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

Browse the available tokens and look for this one: article. This is the token for the currently loaded node content type.

Add you menu title and the following path: nodes/article

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.

Further information on the topic?

My name is DannySipos and I am a Drupal web developer located in Brussels, Belgium. I work full time (+ some more) on Drupal sites and like to document many of my findings for others to benefit from. This is why you'll see me published here but also on Sitepoint.com, TutsPlus or DigitalOcean.

I work as a full time developer but I am also available as a freelancer. So let me know if I can help you with your project.

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.