Created
April 20, 2015 13:21
-
-
Save oliverthiele/38ea55eae17d59cd5a48 to your computer and use it in GitHub Desktop.
TYPO3 Fluid GravatarViewHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OliverThiele\OtWebsite\ViewHelpers; | |
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper; | |
/** | |
* Class GravatarViewHelper | |
* | |
* @package OliverThiele\OtWebsite\ViewHelpers | |
*/ | |
class GravatarViewHelper extends AbstractFormFieldViewHelper { | |
/** | |
* Initialize arguments. | |
* | |
* @return void | |
*/ | |
public function initializeArguments() { | |
$this->registerTagAttribute('email', 'string', 'Specifies the email', TRUE); | |
$this->registerTagAttribute('size', 'string', 'Size in pixels, defaults to 80px [ 1 - 2048 ]', FALSE, '80'); | |
$this->registerTagAttribute('defaultImageset', 'string', 'Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]', FALSE, 'mm'); | |
$this->registerTagAttribute('rating', 'string', 'Maximum rating (inclusive) [ g | pg | r | x ]', FALSE, 'g'); | |
$this->registerTagAttribute('image', 'bool', 'True to return a complete IMG tag False for just the URL', FALSE, TRUE); | |
// $this->registerTagAttribute('additionalAttributes', 'array', 'Specifies additional attributes', FALSE); | |
} | |
/** | |
* Get either a Gravatar image tag or complete URL for a specified email address. | |
* | |
* @return String containing either just a URL or a complete image tag | |
* @source http://gravatar.com/site/implement/images/php/ | |
*/ | |
function render() { | |
$url = 'http://www.gravatar.com/avatar/'; | |
$url .= md5(strtolower(trim($this->arguments['email']))); | |
$url .= '?s=' . $this->arguments['size'] . '&d=' . $this->arguments['defaultImageset'] . '&r=' . $this->arguments['rating']; | |
if ($this->arguments['image']) { | |
$url = '<img src="' . $url . '"'; | |
foreach ($this->arguments['additionalAttributes'] as $key => $val) { | |
$url .= ' ' . $key . '="' . $val . '"'; | |
} | |
$url .= ' />'; | |
} | |
return $url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment