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
, selectUser 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:
[current-user:uid]
. This is the token for the currently logged in user account ID. - Add your menu title and for the path specify:
user/[current-user:uid]
. - 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
, selectNode 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:
[node:content-type:machine-name]
. This is the token for the currently loaded node content type. - Add you menu title and the following path:
nodes/[node:content-type:machine-name]
- 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.
In Cool Modules | Drupal
var switchTo5x = true;stLight.options({"publisher":"dr-8de6c3c4-3462-9715-caaf-ce2c161a50c"});