Using tokens in a menu link

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by flickerfly on October 18, 2008 at 3:29pm

Update by develCuy: Use the module http://drupal.org/project/menu_token

I have a menu item that I'd like to make different for each user. It's an internal page that accepts &_GET in the URL.

I'm thinking I could skip the user past entering this information in manually by providing the url like http://site.com/?user=loggedinuser&target=somepage. Could I fill this username with the drupal username using a token or are they just not available in the context of menu entries?

How else could I solve this if not with tokens?

Comments

I'm trying to do exactly the same thing

Posted by ebrittwebb on November 4, 2008 at 10:48pm

I'm trying to do exactly the same thing. Would love to see a solution for this!

Erik Britt-Webb
drupal@ebrittwebb.com

+1 on this.

Posted by telemako on January 7, 2010 at 3:57pm

+1 on this.

Did you figure this out?

Posted by arilikeairy on December 3, 2008 at 10:45pm

I'm trying to do the same thing and haven't figured it out yet, did either of you?

Similar solution implemented

Posted by develcuy on January 1, 2009 at 12:52am

Similar solution implemented by menu_token.module.

Happy New Year!

--
Fernando @DevElCuy

Excellent

Posted by ebrittwebb on January 6, 2009 at 12:44am

Look forward to trying it out, but there aren't any downloads available yet. When will they be?

Also, it appears right now you have only a 5.x branch. What about 6.x?

Erik Britt-Webb
drupal@ebrittwebb.com

Downloads available now, D6

Posted by develcuy on January 6, 2009 at 5:04am

Downloads available now, D6 release not in plans but patches are welcome.

--
Fernando @DevElCuy

Has anybody figured this out

Posted by tms8707056 on January 31, 2009 at 10:09pm

Has anybody figured this out for 6.x?

Use a redirect page

Posted by zeedaam on February 23, 2009 at 12:37am

I needed to do this on a site I'm building too. So I created a simple redirect.php page.

<?php
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global
$user;
echo
"<meta http-equiv='refresh' content='0;url=user/" . $user->uid . "/edit'>";
?>

For example I made a link in the nav menu that would take the user directly to their user/edit page. I pointed the menu item to my redirect.php and it sent them to an evaluated redirected url. It's pretty fast and seamless too. Hope this helps until some makes this for Drupal 6.

nice hack

Posted by wreford on May 19, 2010 at 7:14am

Nice simple hack. I'm using it to send users to their system messages page (via notifications). That is a fallback from trying and failing to get Views to make a block that displays a list of the user's unread messages and links to each message.

I would rather there were a built-in way to collect the user id and insert it into a path for a menu item, but I'm not a programmer so can't offer one. Just seems like an obviously useful feature.

I'm looking for the same

Posted by Danny_Joris@dru... on May 21, 2009 at 9:21am

I'm looking for the same thing in D6. I would love to show a menu title as a token. In my case i want to show the user name.

I can help

Posted by nancydru on May 21, 2009 at 2:15pm

The Real Name module can provide that link in the Nav menu title.

Nancy Dru

Me Aliases

Posted by citronica on July 14, 2009 at 4:34pm

Thanks

Posted by sha.ramiai on March 17, 2010 at 10:16am

Thanks heartsutra, exactly what I was looking for

user id in menu

Posted by deodesign on July 2, 2013 at 8:28am

I had no luck with menu token, gave me WSOD.

me aliases worked very easily,

many thanks.

function

Posted by erik seifert on August 7, 2009 at 11:59am


function phptemplate_menu_item_link($link)
{
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
//Make all external Links "_blank"
if ( isset($link['href']) && substr($link['href'], 0, 4) == 'http' )
{
$link['localized_options']['attributes']['target'] = '_blank';
}
//Use Tokens
if ( module_exists('token') )
{
$link['href'] = token_replace( $link['href'],'global' );
$link['title'] = token_replace( $link['title'],'global' );
}
return l($link['title'], $link['href'], $link['localized_options']);
}

Erik


Erik Seifert

Thoughts?

Posted by rjbrown99 on August 11, 2009 at 4:04am

I'm also interested in this (for Drupal 6), but not just for tokens. I'm just trying to pass an argument into a URL based on a view page path that uses an argument. For example, a view path of /documents/% where % is the user's userID. I can then browse to a URL of www.mysite.com/documents/userID to see the view of that user's documents. Now I want that view in a menu which is where I run into the challenge since the path is different per URL. The menu will only appear on the page of the user that 'owns' that view, so I can get the argument to pass to the menu without a hitch. But how do I pass it to the menu? My problem is that Drupal's menuing system does not seem to accept /documents/% as a path and instead kicks back an invalid path error when I try to add that as a menu item in Admin -> Site Building -> Menus.

The Me module won't work in this case since it deals with the logged in user, and I need this to work for any userID that is passed as an argument in the URL. I looked at Path Redirect but this also didn't seem like the right fit. Menu token would work great by pulling a userID token, but it's D5 only so it's not an option for me. That brought me to this page as well as this comment: http://drupal.org/node/201848#comment-761771

Unfortunately, menu_item_link has been removed in D6 so that isn't an option either.

Does anyone happen to have a suggestion in this case? What's the preferred approach to passing a dynamic argument to a Drupal menu in D6?

FWIW, I have full root access to my server with no limitations whatsoever in terms of installing modules, modifying themes, changing configs, etc.

same problem here

Posted by tan1772 on October 28, 2009 at 3:11pm

anyone figured out a way?

alan t

+1 on this.

Posted by doublejosh on November 4, 2009 at 7:15am

+1 on this.

Me three

Posted by rubyji on November 6, 2009 at 4:30am

This should be straighforward! If I can make a view with an argument in the URL, I should be able to make a menu item to point to that view.

Suggestions appreciated...

I have the following in a

Posted by malc_b on November 8, 2009 at 4:44pm

I have the following in a module for all my custom functions (D6 BTW)

function mymodule_menu_link_alter(&$item, $menu) {
if (substr($item['link_path'],0,1) == ':') {
$item['options']['alter'] = TRUE;
}
}

function mymodule_translated_menu_link_alter(&$item, $map) {
global $user;

if (substr($item['link_path'],0,1) == ':') {
$item['href'] = substr($item['link_path'],1).'/'.$user->uid;
}
}

Then the menu I put something like :blog/user . This changes to blog/user/33 for user 33.

The : at the beginning is to fool the menu it to believing it is an external link so it doesn't check it at save time. At run time the translation strips that and adds the current user id. Of course you can expand the processing to change an end token and replace that but since I just want the user id I don't bother.

Of course this only works for D6 menu. href links in the code have to be constructed with php.

Thanks

Posted by ranjeesh on July 28, 2010 at 5:10pm

This works like a charm for my purpose...thank you for mentioning the hook here :)

Thanks

Posted by scott859 on October 10, 2011 at 11:00pm

This worked for me as well, thank you!

This is something I am

Posted by GeekyLass on December 28, 2009 at 11:26am

This is something I am surprised no one has managed. There's Menu Token which is apparently having difficulties porting to D6, and Custom Links which works for D6 but does not integrate with menu.

If I was a stronger coder who understood drupal better I would be working on this.

Can't figure out a way to to

Posted by doublejosh on January 7, 2010 at 4:29am

Can't figure out a way to to it at the module level, but I went for a theme level solution for now...

<?php
function phptemplate_menu_item_link($link) {

if(
$link['link_path'] == 'http://www.tableausoftware.com/community/forums') {
$link['options']['attributes']['target']='_blank';
}
return
l($link['title'],$link['link_path'],array('attributes'=>$link['options']['attributes']) );

}
?>

...however, be aware that this does not rebuild using all options that might be on the link... like fragments and queries, etc.

+1 I went for a theme level

Posted by macramole on March 9, 2010 at 3:58pm

+1

I went for a theme level solution too..

it would also be good to have the ability of making contextual rules for menu items visibility

+1 for me

Posted by maxasus123 on March 25, 2010 at 12:20pm

Need the same for d6

Menu Link when user changes

Posted by danielhonrade on May 5, 2010 at 5:44am

D6 - this is on template.php, this code will get the current user id if your link path is set to the dummy node (for example node/219 or any node), so, you menu link will look like Member Account Information...

function phptemplate_menu_item_link($link) {
global $user;
if($link['link_path'] == 'node/219') { // any node id to hold the dummy path
$link['options']['attributes']['target']='_blank';
$link['link_path'] = 'user/' . $user->uid . '/edit/Member Account Information'; // changes path depending on user id
}
return l($link['title'],$link['link_path'],array('attributes'=>$link['options']['attributes']) );

}

Menu highlighting (active trail)

Posted by kfritsche on May 28, 2010 at 1:22pm

I tried the theme level solution.
Works, but the active-trail would not be set, that i needed to theme the menu list. the active class for the link is set, but i need it also for the li.

i haven't figured out another way yet. hope somebody have a good idea...

Active trail

Posted by rjbrown99 on May 28, 2010 at 4:08pm

Here's your solution for pretty much any active trail issue.

http://drupal.org/project/context

Watch the screencast linked from the project page. It's really awesome and I completely re-did my site, active trail, and blocks with this module.

breadcrumbs

Posted by madjr on November 3, 2010 at 6:12pm

you're talking about the breadcrumbs?

i'll check context as suggested

SimpleMenu Conflict

Posted by thinkyhead on September 1, 2010 at 2:20am

If you're using the theme function override method above to expand tokens in your menu labels, and you're also using the SimpleMenu module, this note is for you.

The latest version of SimpleMenu (6.x-1.13) skips the menu theming functions by default, so you may notice your [token] shows up in the menu instead of the token value after an update. To fix this issue, go to the SimpleMenu Settings form and check the box labeled "Allow Simplemenu to call the theme() function."

Peace out!

|
| thinkyhead was here
|

subscribe

Posted by jdln on September 8, 2010 at 2:24pm

subscribe

Solution for /user/xxx

Posted by Daniel Norton on September 16, 2010 at 7:52pm

Put this in your settings.php and it will rewrite /user/non-numeric as user/uid/non-numeric:

function custom_url_rewrite_inbound(&$result, $path, $path_language) {
global $user;
if (($user->uid) && (arg(0,$path) === "user") && !is_numeric(arg(1,$path))) {
$args = arg(NULL,$path);
if ((isset($_GET['q'])) && ($_GET['q'] == $path)) {
array_shift($args);
$result = "user/$user->uid/" . implode("/",$args);
}
else {
$result = url("<front>",array('absolute'=>TRUE)) . implode("/",$args);
}
}
}

CAVEATS:

  1. This won’t work in all configurations. YMMV.
  2. When this is called by the menu system to validate a URL, it returns an absolute URL (http://...), and the menu system will store the absolute result. This bypasses all local access validation and the menu item will not be automatically hidden if the referenced URL is unavailable to the current (logged-in or anonymous) user.

Tokenize Request Parameters

Posted by atheneus on October 22, 2010 at 6:48am

http://drupal.org/project/token_request_params

If you are using tokens you can use Tokenize Request Parameters module along with the Menu Token module (http://drupal.org/project/menu_token) to configure this sort of functionality without the need to code a new module. Tokenize Request Parameter allows you to define what URL parameters to convert into tokens. It makes the tokens available to any token module that consumes tokens (Token Filter, Token Menu, Rules, etc.)

Hope that helps.

nice

Posted by madjr on November 3, 2010 at 5:54pm

i'll try those suggestions

I'm trying to do a similar thing with [nid]

Posted by roxflame on February 10, 2011 at 12:38am

I'm currently trying to find a solution that will allow me to create a menu item as follows
og/users/[nid]
(where [nid] is the node id of the organic groups node the user is currently viewing)

I've tried all sorts of things, and the php redirect option mentioned above sounds like it may be just the ticket, unfortunately, as I'm not a PHP programmer I wouldn't really know where to start!

Any help would be massively appreciated!

use rules

Posted by aerodub on May 9, 2011 at 11:17am

if you want to have a redirect based on uid, you can use Rules.
simply load the user and then redirect to the url using the available token [user:uid]

Use Menu token module

Posted by offerman on August 8, 2011 at 8:49am

I had the very same problem (using D7).
Solved it by deploying the Menu token module. It shows a neat overview of the available tokens in the menu editor.
I now link to the user's IMCE page using this path:
user/[current-user:uid]/imce

I'm trying to do the same thing and menu token seems to work, but when I use a token in the link, the menu choice doesn't appear (even for administrator). Any thoughts?

The problem I am seeing is the the standard menu items for "Create a new blog entry" and "My Blog" don't work and just take you to the main blog page. So, I'm trying to use menu token to create a "My Blog" link that works.

I am using the menu token

Posted by jshamley (not verified) on September 8, 2011 at 7:57pm
jshamley's picture

I am using the menu token module with no issues. I think the tokens have changed. I used 'user/[user-id]/example' to get mine to work.

It's possible to use a token

Posted by laraz on November 28, 2011 at 5:55pm

It's possible to use a token in a shortcut?

Shortcut?

Posted by thinkyhead on November 28, 2011 at 6:29pm

What do you mean by "shortcut"? Can you give an example of what you would like to see?

|
| thinkyhead was here
|

In drupal 7, in the toolbar I

Posted by laraz on November 29, 2011 at 8:13am

In drupal 7, in the toolbar I want a shortcut that link to path="user/[uid]/orders"

Thanks.

Use the menu token module..

Posted by unc0nnected on January 8, 2012 at 8:02pm

Use the menu token module.. simple and beautiful little tool that does the trick

http://drupal.org/project/menu_token

Tokens

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /