国外设计欣赏网站 - DOOOOR.com

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,微信登陆

搜索

[Drupal教程] ..Drupal7中用户头像设置方法

[复制链接]
发表于 10-13-2011 02:39 | 显示全部楼层 |阅读模式
前几天碰到一个制作用户头像的东西,也就是当用户登录后需要打印其头像,如下图所示4 b8 Z1 H4 X+ [+ |. x
虽然在drupal6中做过类似的东西,但是换到drupal7中就不会有效果了,几经寻找,今天终于给弄出来了。稍做整理后放上来跟大家一起共享。3 q% d% G  V2 e: @8 M8 f/ x' p
首先来看看其在drupal6的实现方法,主要分两大步,第一就是在template.php中修改
0 ]+ ^: i5 K) @5 g; ~+ Q- Hfunction yourthemename_preprocess_page(&$vars) { //code}
, T2 {8 X* g$ F3 e/ `5 E我们在上面这个函数加入下面的代码:
0 }* |0 S# a; n1 J8 X  [7 }$ tfunction yourthemename_preprocess_page(&$vars) {if ($user->uid > 0) {    $vars['user_name'] = $user->name;    $profile = content_profile_load('profile', $user->uid);    $picture_path = $profile->field_user_picture[0]['filepath'];    if (empty($picture_path)) {      $fields = content_fields('field_user_picture');      $picture_path = $fields['widget']['default_image']['filepath'];    }    $vars['user_picture'] = l(theme('imagecache', 'user32x32', $picture_path), "user/{$user->uid}", array('html'=>true));  }}
3 `+ |9 |6 h' E  d: Z然后我们需要在page.tpl.php中调出用户头像
- ~: h! R" a5 X5 A2 L# F* {( {2 E<?php print $user_picture; ?>
8 `) B* s/ r2 u( ~# f5 G: g, d5 v上面我简单的列出在drupal6中的实现的方法,了解一下后,我们进入今天的主题,看看在drupal7中如何制作出上图的效果。实质上是没有多大的区别,只是在drupal7中调用文件的路径和方式已改变,另外还有一点就是为了不影响别的地方用户头像大小,我们在这里写死了一个image style样式,这样一来,我们就需要先创建一个image style。具体创建image style的详细方法,我在《Drupal7基础教程--用户(三)》有介绍过,感兴趣的朋友可以进去看看,在这我只是简单的说一下,在地址栏中直接输入"admin/config/media/image-styles/add",你会进入到下图的页面位置:
  C  O8 ?' R1 \% I5 _, A% j
在“style name”中输入您想设置的名字,这里设置的是userheader,点create new style你就可以创建一个image style的样式。创建好样式后,我们后面的工作和在drupal6下基本上是一样的,首先在template.php中加入下面的代码:' P$ h- ^" C; \: A3 |" e
function html5_preprocess_page(&$variables) { // Get user add user picture global $user; if ($user->uid > 0) {   $user_info = user_view($user);   $variables['user_name'] = $user->name;  //设置用户名   $filepath = $user->picture->uri;  //设置用户头像路径   $style = 'userheader'; //设置用户头像样式,这里需要跟刚才创建的image style名一致   if (module_exists('image') && file_valid_uri($filepath)) {     $variables['user_picture'] = theme('image_style', array('style_name' => $style, 'path' => $filepath, ));   }   else {     $variables['user_picture'] = theme('image', array('path' => $filepath, ));   } }    }+ H* c0 l) t$ @) u2 s& w
现在就是最后一步了,在page.tpl.php中打印出用户头像就可以了:
) x( ?  }! @: E) [9 ~. A" i<?php print $user_picture; ?>
" {$ p3 i1 X( p6 \1 d+ s9 K& X. O我们这个例子不单打印用户头像,还 创建了两个链接,现在一起贴上来给大家参考:3 D- l8 ~6 J" ?
<?php if($user->uid): ?>  <aside class="login-bar c4">    <span>Welcome:<?php print l($user->name,'user/'.$user->uid); ?></span> <?php print l("logout","user/logout"); ?>    <p><?php print $user_picture; ?></p>    </aside>           <?php else: ?>   <aside class="login-bar c4">    <?php print l("Login","user/login"); ?> | <?php print l("Singup","user/register"); ?>   </aside><?php endif; ?>4 ?0 W) T7 b' d7 ^) d1 ^4 U* y+ u
其实这一部分内容实现方法,大家还可以参考前面有一篇《玩转Drupal7的User Login》,这样你对这一部分内容就更了解了。大家保存并清除缓存,重新刷新页面就可以看到开文图片所示的效果了。
- o, j$ l5 `! h# d$ Q- q另外还有一种方法就是使用views来创建,如下所示7 C" f. n& Q* G: D: f0 X4 h
不过用views所创建有一个不太好的地方就是需要在用户头像那里先设置好头像的image style,换句话说就是不能按照不同地方放置不同的image style。所以我个人还是偏向于第一种方法。接着把views创建出来的block放到相应的位置就OK了。这样我们如何设置用户头像就介绍完了,如果大家有更好的方法,可以一起探讨学习。
; x7 Y$ U5 i- p# q7 C" e' S如需转载请注明出处:W3CPLUS
. s$ i) u- u) F8 w/ D2 U% e4 n* L
6 p: a1 M  P( M/ p8 L1 J/ \9 i* S& J5 t9 A7 x$ J5 N9 r8 `
& R4 @5 b' d) H; R
# m/ P( ]) h/ y- \+ s: T) \0 L+ }  u
5 U: G0 H$ A3 C: n& y

|2011-2026-版权声明|平台(网站)公约|DOOOOR 设计网 ( 吉ICP备2022003869号 )

GMT+8, 2-14-2025 23:43 , Processed in 0.443140 second(s), 103 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表