You are here

function gravatar_comment in Gravatar integration 5

Implementation of hook_comment().

File

./gravatar.module, line 93
Integrates gravatar service for comment user pictures.

Code

function gravatar_comment(&$comment, $op) {
  $account = user_load(array(
    'uid' => $comment->uid,
  ));

  // allowed to use gravatar (for ananymous and users) and enabled (for users)
  if (user_access('use gravatar', $account) && (!user_access('disable own gravatar', $account) || !isset($account->gravatar) || $account->gravatar)) {
    switch ($op) {
      case 'view':

        // hack for authenticated users
        if ($account->uid) {
          $comment->mail = $account->mail;
        }

        // check email address
        if (!empty($comment->mail)) {

          // Gravatar url
          $grav_url = _gravatar_get_gravatar(array(
            'mail' => $comment->mail,
          ));

          // set Gravatar for template
          $comment->gravatar = theme('gravatar', $grav_url, $comment->name, $comment->homepage);

          // if theme dosen't support Gravatar, insert picture in comment text
          if (gravatar_var('prepend')) {
            $comment->comment = $comment->gravatar . $comment->comment;
          }
        }
        break;
      case 'form':
        $comment['mail']['#description'] .= '<br />' . t('If you have a <a href="@gravatar-website">Gravatar</a> account, used to display your avatar.', array(
          '@gravatar-website' => url('http://www.gravatar.com'),
        ));
        return $comment;
        break;
    }
  }
}