Using tokens in a menu link

flickerfly's picture

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

ebrittwebb's picture

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.

telemako's picture

+1 on this.

Did you figure this out?

arilikeairy's picture

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

Similar solution implemented

develCuy's picture

Similar solution implemented by menu_token.module.

Happy New Year!

--
develCuy's fruits
(3 John 1:2) Dear friend, I pray that you may enjoy good health and that all may go well with you, even as your soul is getting along well.

--
Un drupalero latino,
Trabajando en Dilygent y
Blogueando en http://develCuy.com

Excellent

ebrittwebb's picture

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

develCuy's picture

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

--
develCuy's fruits
(3 John 1:2) Dear friend, I pray that you may enjoy good health and that all may go well with you, even as your soul is getting along well.

--
Un drupalero latino,
Trabajando en Dilygent y
Blogueando en http://develCuy.com

Has anybody figured this out

tms8707056's picture

Has anybody figured this out for 6.x?

Use a redirect page

zeedaam's picture

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

wreford's picture

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

Danny_Joris@drupal.org's picture

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

NancyDru's picture

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

Nancy Dru

Me Aliases

citronica's picture

Try the me aliases module.

Thanks

sha.ramiai's picture

Thanks heartsutra, exactly what I was looking for

user id in menu

deodesign's picture

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

me aliases worked very easily,

many thanks.

function

Erik Seifert's picture


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
b-connect GmbH
http://www.b-connect.de

Thoughts?

rjbrown99's picture

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

tan1772's picture

anyone figured out a way?

alan t

+1 on this.

doublejosh's picture

+1 on this.

Me three

rubyji's picture

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

malc_b's picture

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

ranjeesh's picture

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

Thanks

scott859's picture

This worked for me as well, thank you!

This is something I am

GeekyLass's picture

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

doublejosh's picture

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

macramole's picture

+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

maxasus123's picture

Need the same for d6

Menu Link when user changes

danielhonrade's picture

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)

kfritsche's picture

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

rjbrown99's picture

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

madjr's picture

you're talking about the breadcrumbs?

i'll check context as suggested

SimpleMenu Conflict

thinkyhead's picture

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

jdln's picture

subscribe

Solution for /user/xxx

Daniel Norton's picture

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

atheneus's picture

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

madjr's picture

i'll try those suggestions

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

roxflame's picture

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

aerodub's picture

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

offerman's picture

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

wheresjbob's picture

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

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

laraz's picture

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

Shortcut?

thinkyhead's picture

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

laraz's picture

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

Thanks.

Use the menu token module..

unc0nnected's picture

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: