Drupal - hook_menu_alter - 添加访问回调以检查查看的用户配置文件不是当前用户
我无法理解这个...
我在用户个人资料上添加了“联系人”选项卡。我想要做的是如果用户配置文件不属于登录用户则隐藏它。
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
$user:这是当前用户,请注意全局语句,以便它在您的函数中可见
$account:这是传递给您的函数的用户帐户。需要重命名,因为 $user 已为当前登录的用户保留。
Try this:
$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.
这应该可以解决问题:
除非登录用户的 ID 与 user//contacts 相同,否则将返回 FALSE,从而隐藏菜单链接。您不想将 $user 与访问参数进行比较,因为该参数将是用户 ID,而不是用户对象。 $user->uid 是用户 ID。
This should do the trick:
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.
好的,谢谢大家让我更接近解决方案。我发现如果我单独引用
$items
变量并且不使用数组,它就会起作用......???所以不是:
...但是:
...并使用马特的函数:
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:
...but:
...and using Matt's function: