开发者

Drupal - hook_menu_alter - add access callback to check viewed user profile is not current user

开发者 https://www.devze.com 2023-02-14 16:48 出处:网络
Can\'t get my head around this... I\'ve added a \'contacts\' tab on user profiles. What I want to do is hide this if the user profile does NOT belong to 开发者_开发问答the logged in user.

Can't get my head around this...

I've added a 'contacts' tab on user profiles. What I want to do is hide this if the user profile does NOT belong to 开发者_开发问答the logged in user.

I've got this in an implementation of hook_menu_alter:

$items['user/%views_arg/contacts'] = array(
           'access callback'=>'current_user_hide_tabs', 
           'access arguments'=>array(1), 
        ); 

I just can't seem to get the corresponding function to work:

function current_user_hide_tabs($user) {
  return $user->uid != $account->uid //???
} 

Cheers!

(I've checked that the tab is actually being accessed after asking an older question.)


Try this:

function current_user_hide_tabs($account) {
  global $user;
  return $user->uid != $account->uid;
}

$user: This is the current user, note the global statement so that it is visible inside your function $account: This is the user account passed to your function. Needs to be renamed because $user is already reserved for the currently logged in user.


This should do the trick:

$items['user/%/contacts'] = array(
  'access callback'=>'current_user_hide_tabs', 
  'access arguments'=>array(1), 
); 

function current_user_hide_tabs($uid) {
  global $user;

  return $user->uid == $uid;
} 

Will return FALSE unless the logged in user's ID is the same as user//contacts thus hiding the menu link. You don't want to compare $user to the access argument because the argument will be a user ID, not a user object. $user->uid is the user ID.


OK, thanks guys for getting me closer to a solution. I found out that it worked if I reference the $items variables individually and don't use an array...???

So not:

$items['user/%views_arg/contacts'] = array(
   'access callback'=>'current_user_hide_tabs', 
   'access arguments'=>array(1), 
); 

...but:

$items['user/%views_arg/contacts']['access callback'] = 'current_user_hide_tabs';
$items['user/%views_arg/contacts']['access arguments'] = array(1); 

...and using Matt's function:

function current_user_hide_tabs($uid) {
  global $user;
  return $user->uid != $uid;
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消