2010-01-11 81 views
4

我是一个节点上显示userpicture这个代码在这里:Drupal的:检查是否用户具有图片

<?php 
    $user_load = user_load($node->uid); 
    $imgtag = theme('imagecache', 'avatar_node', $user_load->picture, $user_load->name, $user_load->name); 
    $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE); 
    print l($imgtag, 'u/'.$user_load->name, $attributes); 
?> 

这个伟大的工程,但如果用户没有图片,其中情况看起来很奇怪。

如果用户没有图片,我该如何加载默认图片。 我不相信我可以访问此块中的$图片。

在此先感谢。

回答

6

    $user_load = user_load($node->uid); 
    $picture = $user_load->picture ? $user_load->picture : variable_get('user_picture_default', ''); // 
    $imgtag = theme('imagecache', 'avatar_node', $picture, $user_load->name, $user_load->name); 
    $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE); 
    print l($imgtag, 'u/'.$user_load->name, $attributes); 

如果你默认的图片复制到文件的目录,你可以通过http://api.drupal.org/api/function/file_directory_path/6

+0

而不是手动路径为默认图片,是不是有办法获得在drupal用户设置的默认图片集? 谢谢。 – Jourkey 2010-01-11 08:25:58

+2

variable_get('user_picture_default','')将获得默认的图片URL。 (编辑答案包括此) – 2010-01-11 10:13:16

2

确定它这是我使用的是什么了类似的情况:

 if (!empty($user->picture) && file_exists($user->picture)) { 
     $picture = file_create_url($user->picture); 
     } 
     else if (variable_get('user_picture_default', '')) { 
     $picture = variable_get('user_picture_default', ''); 
     } 

     if (isset($picture)) { 

     $alt = t("@user's picture", array('@user' => $user->name ? $user->name : variable_get('anonymous', t('Anonymous')))); 
     $picture = theme('image', $picture, $alt, $alt, '', FALSE); 
     if (!empty($user->uid) && user_access('access user profiles')) { 
      $attributes = array(
      'attributes' => array('title' => t('View user profile.')), 
      'html' => TRUE, 
     ); 
      echo l($picture, "user/$user->uid", $attributes); 
     } 
     } 

它是从http://api.drupal.org/api/drupal/modules%21user%21user.module/function/template_preprocess_user_picture/6改编

1

在Drupal 7中我使用:

global $user; 
file_create_url(file_load($user->picture)->uri)