Make theme_user_picture to support custom image_styles

Public

put into template.php of your theme

Get raw version
php
  1. /**
  2.  * Process variables for user-picture.tpl.php.
  3.  *
  4.  * The $variables array contains the following arguments:
  5.  * - $account: A user, node or comment object with 'name', 'uid' and 'picture'
  6.  * fields.
  7.  *
  8.  * @see user-picture.tpl.php
  9.  */
  10. function yourthemename_preprocess_user_picture(&$variables) {
  11. $variables['user_picture'] = '';
  12. if (variable_get('user_pictures', 0)) {
  13. $account = $variables['account'];
  14. if (!empty($account->picture)) {
  15. // @TODO: Ideally this function would only be passed file objects, but
  16. // since there's a lot of legacy code that JOINs the {users} table to
  17. // {node} or {comments} and passes the results into this function if we
  18. // a numeric value in the picture field we'll assume it's a file id
  19. // and load it for them. Once we've got user_load_multiple() and
  20. // comment_load_multiple() functions the user module will be able to load
  21. // the picture files in mass during the object's load process.
  22. if (is_numeric($account->picture)) {
  23. $account->picture = file_load($account->picture);
  24. }
  25. if (!empty($account->picture->uri)) {
  26. $filepath = $account->picture->uri;
  27. }
  28. }
  29. elseif (variable_get('user_picture_default', '')) {
  30. $filepath = variable_get('user_picture_default', '');
  31. }
  32. if (isset($filepath)) {
  33. $alt = t("@user's picture", array('@user' => format_username($account)));
  34. // If the image does not have a valid Drupal scheme (for eg. HTTP),
  35. // don't load image styles.
  36. $style = variable_get('user_picture_style', '');
  37. if (isset($variables['image_style'])) {
  38. $style = $variables['image_style'];
  39. }
  40. if (module_exists('image') && file_valid_uri($filepath) && isset($style)) {
  41. $variables['user_picture'] = theme('image_style', array(
  42. 'style_name' => $style,
  43. 'path' => $filepath,
  44. 'alt' => $alt,
  45. 'title' => $alt
  46. ));
  47. }
  48. else {
  49. $variables['user_picture'] = theme('image', array('path' => $filepath, 'alt' => $alt, 'title' => $alt));
  50. }
  51. if (!empty($account->uid) && user_access('access user profiles')) {
  52. $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
  53. $variables['user_picture'] = l($variables['user_picture'], "user/$account->uid", $attributes);
  54. }
  55. }
  56. }
  57. }