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.

My code so far is the following one.

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 am home"; however, if I view another profile this doesn't change as it should. How can I fix this?

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. –  au_stan Jan 10 '13 at 20:55

3 Answers 3

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 $profileURL;
}
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) {
      //Declare $user to get the global $user object
      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
1  
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

You can use the Entity Visits module for your purpose. Excerpt from its project page:

... tracks the users who viewed the entity.

You have to apply the patch included in the issue about "Module does not register user visits" for it to work properly.

You can also use the User Visits Advanced module for the same purpose.

The User Visits Advanced module is a contrib module for the User Visits module. It is extremely useful for social networking sites who wish to provide their users with statistics about the number of times a the users profile page is being viewed and by which other users. See the provided screen shot how the block of this module can look like.

This module comes with its own database table to store statistics per user profile (uid). At cron time, the data from the user_visits table is collected, aggregated and stored in the user_visits_adv table. It stores

  • the total number of visits per uid
  • the number of visits of the X past days per uid
  • the most recent visitors uids of the past Y hours
share|improve this answer
    
Welcome to Drupal Answers! The question is about code the OP wrote. Suggesting other modules is not answering the question. –  kiamlaluno May 25 at 12:09

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.