在 Drupal 7 中向 $user 添加头像

发布于 2024-10-21 00:34:25 字数 988 浏览 1 评论 0原文

我创建了一个简单的模块,用于通过覆盖 sites/default/modules/game.module 工作正常。

然而,我需要通过 FlashVars 参数将用户头像以及性别和城市(我已在注册表中添加了 2 个必填字段)传递到我所在块中的 Flash 游戏。

所以我试图重载 hook_user_load,因为我认为这是从数据库启动 $user 对象后向其添加属性的方法(这可能在用户登录或更改他/她的个人资料数据时发生?) :

function game_user_load($users) {
  global $user;
  $uid = $user->uid;

  $result = db_query('select filename from {file_managed} where uid=:uid', array(':uid' => array($uid)));
  $avatar = $result->fetchField();
  $users[$uid]->avatar = $avatar;

  drupal_set_message("<pre>$uid: $avatar</pre>\n");
  print_r($users);
}

不幸的是,我在网页中没有看到上面最后两行产生的输出,

我做错了什么?

谢谢你! 亚历克斯

I've created a simple module for displaying a flash game in a custom block by overwriting game_block_view() and game_block_info() in the sites/default/modules/game.module and it works ok.

I need however to pass user avatar and also gender and city (I've added the 2 mandatory fields to the registration form) through the FlashVars-parameter to the flash game in my block.

So I'm trying to overload the hook_user_load, because I suppose that this is the method where you add properties to the $user object after it has been initiated from the database (this probably happens when the user logins or alters his/her profile data?):

function game_user_load($users) {
  global $user;
  $uid = $user->uid;

  $result = db_query('select filename from {file_managed} where uid=:uid', array(':uid' => array($uid)));
  $avatar = $result->fetchField();
  $users[$uid]->avatar = $avatar;

  drupal_set_message("<pre>$uid: $avatar</pre>\n");
  print_r($users);
}

Unfortunately I see no output produced by the last 2 lines above in the web page

What am I doing wrong?

Thank you!
Alex

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

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

发布评论

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

评论(1

梦屿孤独相伴 2024-10-28 00:34:25

像这样的东西应该有效:

global $user;

// $account is now a fully loaded user object.
$account = user_load($user->uid);

// Your field name is probably 'field_avatar'.  
if ($avatar = field_get_items('user', $account, 'field_avatar')) {  
  dpm($avatar); // only works with devel.module, strongly suggested!  
}

Something like this should work:

global $user;

// $account is now a fully loaded user object.
$account = user_load($user->uid);

// Your field name is probably 'field_avatar'.  
if ($avatar = field_get_items('user', $account, 'field_avatar')) {  
  dpm($avatar); // only works with devel.module, strongly suggested!  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文