How to avoid Drupal 7 user ID in user path?

387 Views Asked by At

I have the following in my theme:

$items['user/%user/sms-services'] = array(
    'title' => t('SMS Services'),
    'page callback' => 'sms_user_page',
    'page arguments' => array(1),
    'access callback' => 'user_is_logged_in',
    'type' => MENU_LOCAL_TASK,
); 

It works great in that it:

  1. Creates a url /user/USER_ID/sms-services
  2. Creates a tab on the /user page

However, I want to get rid of the USER ID part. I.e. The link must work as:

/user/sms-services

And /user/USER_ID/sms-services must redirect to /user/sms-services

And /user/USER_ID must redirect to /user

Is there an easy way to do this?

UPDATE

I used the "me aliases" module to accomplish much of this, but it's still not a very elegant solution, because now i'm stuck with TWO urls working:

/user/me/some-action

AND

/users/393/some-action

When all I really wanted was:

/user/some-action

Anyone got any ideas?

1

There are 1 best solutions below

1
On

Maybe you can do sometinhg like this instead. Check if the user entering user/sms-services is logedin by using user_is_logged_in, if true use the global $user for further stuff. Maybe something like this

$items['user/sms-services'] = array(
    'title' => 'SMS Services',
    'page callback' => 'sms_user_page',
    'access callback' => 'user_is_logged_in',
    'type' => MENU_LOCAL_TASK,
); 

function sms_user_page(){
    if(user_is_logged_in){//Just realize, with 'access callback' => 'user_is_logged_in' in the menu item array this if is unnecesary
      global $user;
      $user->uid;

      //Do stuff
    }
}

And maybe make a drupal_goto() from the old ones (menu items with the user wildcard to the new ones)

Very importat:

Do not use t() function in your menu item. By defualt drupal will pass the title string into the function t(). You can change that behavior by setting a new 'title callback' in your menu item array

See hook_menu from drupal.org