Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm relatively new to Drupal and have created a module that among other things uses hook_menu() to add a link to a user navigation "my gallery" this all works well except this link appears when a user views another user's profile. So what i want to do is add some code to tell Drupal not to display this link when a user is not on their own profile.

I'm guessing I need to change the access callback from user_access to a custom callback that carries out this check. Any help would be greatly appreicated.

EDIT my code so far is

global $user;
    //get name of current user
    $currentUser = $user->uid;
    //get profile url
    $profileUrl = arg(1);
    // compare usernames to determine whether the user is on their own profile.
    if($currentUser == $profileUrl)
    {
        //user is on their profile, display link
        $profileUrl = "i'm home";
    }
    else
    {
        $profileUrl = "away";
    }
    $items['user/%user/gallery'] = array(
        'title'            => $profileUrl
       ,'page callback'    => 'drupal_goto'
       ,'page arguments'   => array('gallery')
       ,'access callback'  => 'user_access'
       ,'access arguments' => array('access content')
       ,'type'             => MENU_LOCAL_TASK
    );    
    return $items;

The problem is it seems to get fixated, so if i start on the admin profile it correctly displays i'm home, however if i view another profile this doesn't change as it should. ANy ideas?

EDIT 2 Thanks mohit_rocks, your idea of using the title callback helped me get to the answer. For those interested I set up an access callback to perform the check dynamically.

share|improve this question
    
check the global $user. You can then compare that to the url. if they are the same then you are viewing your own profile. –  austin Jan 10 '13 at 20:55
    
May you show the code you are using? I am not clear what you mean by, "add a link to a user navigation." –  kiamlaluno Jan 10 '13 at 21:34

2 Answers 2

up vote 1 down vote accepted

You can try 'title callback' attribute of hook_menu function.

$items['user/%user/gallery'] = array(
       'title callback' => 'get_title',
       'title arguments' => array(1),
       'page callback'    => 'drupal_goto',
       'page arguments'   => array('gallery'),
       'access callback'  => 'user_access',
       'access arguments' => array('access content'),
       'type'             => MENU_LOCAL_TASK,
    );    

And now you can call the function

function get_title($user_id) {
    global $user;
    $currentUser = $user->uid;
    //get profile url for that you can use $user_id as user id
    $profileUrl = arg(1);
    // compare usernames to determine whether the user is on their own profile.
    if($currentUser == $profileUrl)
    {
        //user is on their profile, display link
        $profileUrl = "i'm home";
    }
    else
    {
        $profileUrl = "away";
    }
    return $prifileURL;
}
share|improve this answer

You can use

hook_user_view

The user's account information is being displayed.

The module should format its custom additions for display and add them to the $account->content array.

e.g

function MYMODULE_user_view($account, $view_mode, $langcode) {
  global $user;
  // Check if logged in user id matches viewed user account
  if ($user->uid == $account->uid)) {
    $account->content['summary']['gallery'] =  array(
      '#type' => 'user_profile_item', 
      '#title' => t('My gallery'), 
      '#markup' => $gallery,  // TODO, get account gallery markup
      '#attributes' => array('class' => array('my-gallery')),
    );
  }
}
share|improve this answer
    
The OP said he is using hook_menu(), which means he is using a menu item that takes to a different page than the user profile. –  kiamlaluno Jan 10 '13 at 21:35
    
Yes, though it sounds like OP is looking to add a gallery to a user profile, e.g "except this link appears when a user views another user's profile" hence the alternative solution to show the gallery on the user profile. –  David Thomas Jan 10 '13 at 21:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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