How to avoid Drupal 7 user ID in user path?

My theme has the following:

$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 because it:

  • Creates url / user / USER_ID / sms-services
  • Creates a tab on the user / user page

However, I want to get rid of the USER ID part. That is, the Link should work like:

/user/sms-services

      

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

And / user / USER_ID needs to be redirected to / user

Is there an easy way to do this?

UPDATE

I used the "me aliases" module to accomplish most of this, but this is not a very neat solution yet, because now I am stuck with two urls:

/user/me/some-action

      

and

/users/393/some-action

      

When all I really wanted was:

/user/some-action

      

Does anyone have any idea?

+3


source to share


1 answer


Maybe you can do sometinhg like this. Check if the user entering user / sms services is logged in with user_is_logged_in, if true uses the global $ user for further use. 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 do drupal_goto () from old ones (user wildcard menu items for new ones)



Very importat:

Don't use the t () function in your menu item. Defualt drupal passes the title string to the t () function. You can change this behavior by setting a new "callback" in your menu item array

See hook_menu from drupal.org

+1


source







All Articles