我需要一个插件函数来检查显示的帐户是用户的帐户还是其他帐户的帐户。我正在使用这个函数:
global $user; $account;
$account = user_load(array('uid' => arg(1)));
if ( $user->uid == $account->uid ) {
}
我正在模块内执行此操作,但它不起作用。当我转到我的个人资料时,我从未看到该函数的输出。
为什么?
编辑
此代码的原始上下文:
function tpzclassified_menu() {
global $user;
$account = user_load(array('uid' => 1));
$account = user_load(array('uid' => 1));
if ($user->uid == $account1->uid) {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
}
return $items;
}
最佳答案
I need a function for a plugin to check, if the shown account is the user's account or that from another one.
报告的代码正在做的是验证登录用户是否是 Drupal super 用户(也称为用户#1)。如果这是您真正需要的,则无需调用 user_load()
来加载该用户帐户的用户对象。使用以下代码就足够了:
global $user;
if ($user->uid == 1) {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
}
return $items;
在 Drupal 中,不存在具有相同用户 ID 的两个用户,1 是 Drupal super 用户的用户 ID(也称为用户 #1,因为其用户 ID 为 1)。
这个解决方案的问题是,从 Drupal 6 开始,菜单回调被缓存;有条件地添加菜单回调不会产生任何效果,因为只有在安装新模块或更新模块(并调用 update.php)时才会清除菜单缓存。强制 Drupal 清除菜单缓存的唯一方法是使用以下代码:
if (!variable_get('menu_rebuild_needed', FALSE)) {
variable_set('menu_rebuild_needed', TRUE);
}
如果您想检查当前登录的用户是否正在访问自己的帐户,您可以使用以下代码:
function tpzclassified_menu() {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
return $items;
}
没有理由使用自定义函数,如 user_view_access()
已经检查当前用户是否正在查看自己的帐户。
function user_view_access($account) {
return $account && $account->uid &&
(
// Always let users view their own profile.
($GLOBALS['user']->uid == $account->uid) ||
// Administrators can view all accounts.
user_access('administer users') ||
// The user is not blocked and logged in at least once.
($account->access && $account->status && user_access('access user profiles'))
);
}
关于drupal - 检查显示的帐户是否是用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3505911/