Jump to content

Pulling in Gravatar Avatars


Recommended Posts

Hello PW Community,

I'm trying to modify a small module I found to pull in Gravatars but I'm having a bit of an issue and was curious if someone has already done a Gravatar implementation, or could look over the module code? It installed no problem, but when I try to use it, nothing works.

Here is the full module:

<?php

/**
 * Module for generating Gravatar URLs for ProcessWire users.
 *
 * Use it like this:
 *
 * Get URL only with all defaults:
 * $user->gravatar()
 *
 * Get image tag with different size:
 * $user->gravatar(array('img' => true, 's' => 200));
 *
 */

class Gravatar extends WireData implements Module {

	public static function getModuleInfo() {
		return array(
			'title' => 'Gravatar',
			'version' => 1,
			'summary' => 'Gravatar hook for users',
			'singular' => true,
			'autoload' => true,
			'icon' => 'smile-o',
		);
	}

	public function init() {
		$this->addHook('User::gravatar', $this, 'methodGravatar');
	}

	public function methodGravatar($event) {

		$u = $event->object;
		if ( ! $u instanceof User) $event->return = false; return;
		if ( ! $u->email) $event->return = false; return;

		$params = ($event->arguments(0) ? $event->arguments(0) : array());
		// Default options
		$defaults = array(
			's' => 100,
			'd' => 'retro',
			'r' => 'g',
			'img' => false,
			'attrs' => array(),
		);

		$opts = array_merge($defaults, $params);
		extract($opts);

		$url = '//www.gravatar.com/avatar/';
		$url .= md5(strtolower(trim($u->email)));
		$url .= "?s=$s&d=$d&r=$r";

		if ($img) {
			$url = '<img src="' . $url . '"';
			foreach ($attrs as $key => $val ) {
				$url .= ' ' . $key . '="' . $val . '"';
			}
			$url .= ' />';
		}
		$event->return = $url;
	}
}

The issue is when I use $user->gravatar() I'm getting back "unknown". I know that the email being used by the current logged-in user (me) does have a Gravatar, but I'm unable to get the URL so I can place it into my template markup.

Any insight or direction would be appreciated. I did a search for Gravatar modules / forum topics but it mostly appears to be linked to the Blog profile that uses Gravatar, not a stand-alone module / implementation.

Regards,

VirtuallyCreative

Link to comment
Share on other sites

The approach is fine, just a couple of minor problems, one being the if($img) conditional. The others are the "return" at the end of the first couple of checks that are exiting right there.

Try this:

<?php

/**
 * Module for generating Gravatar URLs for ProcessWire users.
 *
 * Use it like this:
 *
 * Get URL only with all defaults:
 * $user->gravatar()
 *
 * Get image tag with different size:
 * $user->gravatar(array('img' => true, 's' => 200));
 *
 */

class Gravatar extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Gravatar',
            'version' => 1,
            'summary' => 'Gravatar hook for users',
            'singular' => true,
            'autoload' => true,
            'icon' => 'smile-o',
        );
    }

    public function init() {
        $this->addHook('User::gravatar', $this, 'methodGravatar');
    }

    public function methodGravatar($event) {

        $u = $event->object;
        if ( ! $u instanceof User) return;
        if ( ! $u->email) return;

        $params = ($event->arguments(0) ? $event->arguments(0) : array());
        // Default options
        $defaults = array(
            's' => 100,
            'd' => 'retro',
            'r' => 'g',
            'img' => false,
            'attrs' => array(),
        );

        $opts = array_merge($defaults, $params);
        extract($opts);

        $url = '//www.gravatar.com/avatar/';
        $url .= md5(strtolower(trim($u->email)));
        $url .= "?s=$s&d=$d&r=$r";

        $url = '<img src="' . $url . '"';
        foreach ($attrs as $key => $val ) {
            $url .= ' ' . $key . '="' . $val . '"';
        }
        $url .= ' />';
        
        $event->return = $url;
    }
}

 

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...