Drupal - hook_menu_alter - 添加访问回调以检查查看的用户配置文件不是当前用户

发布于 2024-10-19 21:58:51 字数 598 浏览 4 评论 0原文

我无法理解这个...

我在用户个人资料上添加了“联系人”选项卡。我想要做的是如果用户配置文件不属于登录用户则隐藏它。

我在 hook_menu_alter 的实现中得到了这个:

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

我似乎无法让相应的函数工作:

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

干杯!

(我在询问 旧问题。)

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻身的咸鱼 2024-10-26 21:58:52

试试这个:

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

$user:这是当前用户,请注意全局语句,以便它在您的函数中可见
$account:这是传递给您的函数的用户帐户。需要重命名,因为 $user 已为当前登录的用户保留。

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.

荭秂 2024-10-26 21:58:52

这应该可以解决问题:

$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;
} 

除非登录用户的 ID 与 user//contacts 相同,否则将返回 FALSE,从而隐藏菜单链接。您不想将 $user 与访问参数进行比较,因为该参数将是用户 ID,而不是用户对象。 $user->uid 是用户 ID。

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.

£冰雨忧蓝° 2024-10-26 21:58:52

好的,谢谢大家让我更接近解决方案。我发现如果我单独引用 $items 变量并且不使用数组,它就会起作用......???

所以不是:

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

...但是:

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

...并使用马特的函数:

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

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;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文