Drupal:检查用户是否有图片

发布于 2024-08-17 06:23:38 字数 503 浏览 7 评论 0原文

我在此处使用以下代码在节点上显示用户图片:

<?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);
?>

这非常有效,除非用户没有图片,在这种情况下它看起来很奇怪。

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

提前致谢。

I'm displaying the userpicture on a node with this code here:

<?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);
?>

This works great, except if the user doesn't have a picture, in which case it looks strange.

How do I load the default picture if the user doesn't have a picture.
I don't believe I have access to $picture in this block.

Thanks in advance.

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

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

发布评论

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

评论(3

旧情勿念 2024-08-24 06:23:38

    $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


    $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);

if you copy default picture to files directory, you can determine it via http://api.drupal.org/api/function/file_directory_path/6

じ违心 2024-08-24 06:23:38

这就是我在类似情况下使用的:

      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

This is what I'm using for a similar situation:

      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);
        }
      }

It is adapted from http://api.drupal.org/api/drupal/modules%21user%21user.module/function/template_preprocess_user_picture/6

追我者格杀勿论 2024-08-24 06:23:38

在 Drupal 7 中我使用:

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

In Drupal 7 I use:

global $user;
file_create_url( file_load($user->picture)->uri)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文