Drupal: التحقق لمعرفة ما إذا كان المستخدم لديه صورة

StackOverflow https://stackoverflow.com/questions/2040385

  •  19-09-2019
  •  | 
  •  

سؤال

أقوم بعرض UserPicture على عقدة مع هذا الرمز هنا:

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

هذا يعمل بشكل رائع، إلا إذا لم يكن لدى المستخدم صورة، في هذه الحالة تبدو غريبة.

كيف يمكنني تحميل الصورة الافتراضية إذا لم يكن لدى المستخدم صورة. لا أصدق أن لدي حق الوصول إلى الصورة $ في هذه الكتلة.

شكرا لك مقدما.

هل كانت مفيدة؟

المحلول


    $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.

نصائح أخرى

هذا هو ما أستخدمه لوضع مماثل:

      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.

في دروبال 7 أنا استخدم:

global $user;
file_create_url( file_load($user->picture)->uri)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top