3

This is for Drupal 7.

Currently on the default user profile page, the profile picture just links back to their profile. I'd like to change this so it opens a colorbox. Thoughts?

SOLUTION

Add the following to your theme's template.php file.

<?php
function ccom_preprocess_user_picture(&$variables) {
  $variables['user_picture'] = '';
  $original_image = NULL; 
  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));
        $original_image = image_style_url('large', $filepath);
      }
      else {
        $variables['user_picture'] = theme('image', array('path' => $filepath, 'alt' => $alt, 'title' => $alt));
      }
      if (!empty($account->uid) && user_access('access user profiles')) {
        if (isset($original_image)) {
          $attributes = array(
            'attributes' => array('title' => t('View expanded image.'), 'class' => t('colorbox')),
            'html' => TRUE,
          );
          $variables['user_picture'] = l($variables['user_picture'], "$original_image", $attributes);
        }
        else {
          $attributes = array(
            'attributes' => array('title' => t('View user profile.')),
            'html' => TRUE,
          );
          $variables['user_picture'] = l($variables['user_picture'], "user/$account->uid", $attributes);
        }
      }
    }
  }
}
?>

1 Answer 1

1

Theme API is your friend.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.