在 Drupal 7 中向 $user 添加头像
我创建了一个简单的模块,用于通过覆盖 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
全局用户对象不经过hook_user_load(),参见http://api.drupal.org/api/drupal/includes--session.inc/function/_drupal_session_read/7。不要问我为什么,事情就是这样:)
当使用 user_load() 时,任何添加的字段都会自动加载,您不需要为此编写自定义代码。您只需要知道如何访问它们,这有点复杂。
像这样的东西应该有效:
The global user object does not go through hook_user_load(), see http://api.drupal.org/api/drupal/includes--session.inc/function/_drupal_session_read/7. Don't ask me why, that's just the way it is :)
When using user_load(), any added fields will automatically be loaded, you don't need custom code for that. You just need to know how to access them, which is a bit complicated.
Something like this should work: