6.x user.module template_preprocess_user_picture(&$variables)
7.x user.module template_preprocess_user_picture(&$variables)

Process variables for user-picture.tpl.php.

The $variables array contains the following arguments:

  • $account: A user, node or comment object with 'name', 'uid' and 'picture' fields.

See also

user-picture.tpl.php

File

modules/user/user.module, line 1494
Enables the user registration and login system.

Code

function template_preprocess_user_picture(&$variables) {
  $variables['user_picture'] = '';
  if (variable_get('user_pictures', 0)) {
    $account = $variables['account'];
    if (!empty($account->picture)) {

      // @TODO: Ideally this function would only be passed file objects, but
      // since there's a lot of legacy code that JOINs the {users} table to
      // {node} or {comments} and passes the results into this function if we
      // a numeric value in the picture field we'll assume it's a file id
      // and load it for them. Once we've got user_load_multiple() and
      // comment_load_multiple() functions the user module will be able to load
      // the picture files in mass during the object's load process.
      if (is_numeric($account->picture)) {
        $account->picture = file_load($account->picture);
      }
      if (!empty($account->picture->uri)) {
        $filepath = $account->picture->uri;
      }
    }
    elseif (variable_get('user_picture_default', '')) {
      $filepath = variable_get('user_picture_default', '');
    }
    if (isset($filepath)) {
      $alt = t("@user's picture", array(
        '@user' => format_username($account),
      ));

      // If the image does not have a valid Drupal scheme (for eg. HTTP),
      // don't load image styles.
      if (module_exists('image') && file_valid_uri($filepath) && ($style = variable_get('user_picture_style', ''))) {
        $variables['user_picture'] = theme('image_style', array(
          'style_name' => $style,
          'path' => $filepath,
          'alt' => $alt,
          'title' => $alt,
        ));
      }
      else {
        $variables['user_picture'] = theme('image', array(
          'path' => $filepath,
          'alt' => $alt,
          'title' => $alt,
        ));
      }
      if (!empty($account->uid) && user_access('access user profiles')) {
        $attributes = array(
          'attributes' => array(
            'title' => t('View user profile.'),
          ),
          'html' => TRUE,
        );
        $variables['user_picture'] = l($variables['user_picture'], "user/{$account-&gt;<span class="php-function-or-constant property member-of-variable">uid</span>}", $attributes);
      }
    }
  }
}

Comments

deep-silver’s picture

I believe that default user picture should be theme independent
same as site logo, if default avatar picture found in the theme folder it will overwrite the default picture and allow each them to have its own avatar.
allow overwrite of $variables['user_picture_default']

egor.malygin’s picture

If you want default user picture to use default user picture style, you need to specify path like this "public://file.png" (file should be in sites/default/files/file.png) or use this module https://www.drupal.org/project/upload_default_avatar. I wonder why is this not mentioned in the description to the field or somehow. Well, i know why, for people to waste their time on googling around for hours about drupal's "brilliant ui".