0

I run a social movement community site, which features links to the '/user' page, which either shows people the login/registration page, or shows them their user account if they're logged in. If people are logged in, how can I make these links to /user redirect to /user/UID ? (E.g. if I'm the one logged in, I'd like to redirect my visits to http://effectivealtruismhub.com/user to http://effectivealtruismhub.com/user/1 - currently it shows me my user page, but just shows http://effectivealtruismhub.com/user in the address bar.)

This is a valuable feature because people benefit from seeing the URL of their user page, as on this community site and others they'll want to share it with people.

1

If you were handcoding this, you would create a menu path using hook_menu, with %uid as the second parameter. And the page_callback value of the menu item would be the function rendering the user page. You will then use hook_user_login to set the redirect rule.

If you were doing this using contrib modules, I suggest you looking into profile2, it comes with a built in functionality where each user has their own profile page.

edited: The last sentence on your question, makes me led to think that the user/%uid path needs to be accessible users/visitors other than the owner. So let's here is how you would do it.

  1. Create your custom module, let's call it ea_custom for the sake of this example.
  2. Add hook_menu and page_callback to your ea_custom.module and register the path for the individual user page.

    /* Implements hook_menu */ function ea_custom_menu() { $items['users/%user'] = array( //the %user will be used as argument for the page callback 'title' => t('User Profile'), 'page callback' => 'ea_custom_render_userpage', 'page arguments' => array(1), //zero base array. This tells hook menu to first load user with the specified uid specified by %user parameter. 'access callback' => 'user_access', //so non-onwer can also view it 'access arguments' => array('access content'), ); return $items; }

    /**** page callback for users/%user ****/ function ea_custom_render_userpage($user) { //you now have accessed to the user object drupal_set_message('

    ' . print_r($user, TRUE) . '
    ');

    //assemble your output using the values inside $user object $output = $user->uid; //etc.. etc...

    return $output; }

Additional notes: you can use different methods to get values from entity objects such as user and node. I would recommend you to check entity_metadata_wrapper for one. Also, instead of using %user, to get user object, you could opt to use %profile2, since you have that module installed, and it will allows you access to fields you have attached to profile type.

wow, I can't seem to format this properly. Here is the paste bin: http://pastebin.com/kiv6GfPN

  • Thanks, I'll look into the first option. I'm using profile2, but it still has the described issue: if you visit effectivealtruismhub.com/user you only see effectivealtruismhub.com/user in the address bar. – tog22 Oct 31 '14 at 10:37
  • Alas I need some help understanding the 1st option. I don't know what you mean by "with %uid as the second parameter" or "use hook_user_login to set the redirect rule". Here's my code so far, how would I extend it? It's at pastebin.com/mK6cUpFW and pasted here: <?php function ea_hub_support_menu() { $items['user/redirect'] = array( 'page callback' => 'WHAT?' ); return $items; } – tog22 Oct 31 '14 at 10:50
0

You can use the module Login Destination, Website: Login Destination

Once there you can create your the action destination after login and inside there create a code in PHP:

<?php
  global $user;
  return  "user/".$user->uid;
?>

That can work for what you need, let me know !

  • This doesn't sound like it gives me a page I can link to for all users to take them to /user/UID – tog22 Oct 31 '14 at 10:40
  • Login Destination will redirect to whatever page you need ... if you do that PHP Code, that will redirect to your website plus that link after you log in. Example : effectivealtruismhub.com/user/".$user->uid Isn't that what your looking for? – DarkteK Oct 31 '14 at 15:50
  • No, I want to redirect the URL /user to /user/UID, for logged in users. At the very least, I want to be able to have a link - like /user - which will show the page of whatever user is logged in, and have that link then show /user/UID in the address bar. – tog22 Oct 31 '14 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.