diff --git a/Classes/ViewHelpers/GravatarViewHelper.php b/Classes/ViewHelpers/GravatarViewHelper.php
index d237a36..b02a18b 100644
--- a/Classes/ViewHelpers/GravatarViewHelper.php
+++ b/Classes/ViewHelpers/GravatarViewHelper.php
@@ -29,52 +29,106 @@
*
*
*
*/
-class GravatarViewHelper extends AbstractTagBasedViewHelper
+final class GravatarViewHelper extends AbstractTagBasedViewHelper
{
- /**
- * @var string
- */
protected $tagName = 'img';
- public function __construct(private readonly UriFactory $uriFactory)
- {
+
+ public function __construct(
+ private readonly UriFactory $uriFactory,
+ ) {
parent::__construct();
}
public function initializeArguments(): void
{
- $this->registerArgument('emailAddress', 'string', '', true);
- $this->registerArgument('defaultImageUri', 'string', '');
- $this->registerArgument('size', 'int', '');
+ $this->registerArgument(
+ 'emailAddress',
+ 'string',
+ 'Email address used to generate the Gravatar hash',
+ true,
+ );
+
+ $this->registerArgument(
+ 'defaultImage',
+ 'string',
+ 'Gravatar default image (mp, identicon, retro, monsterid, wavatar, robohash, blank)',
+ false,
+ 'mp',
+ );
+
+ $this->registerArgument(
+ 'size',
+ 'int',
+ 'Size of the avatar in pixels',
+ false,
+ 40,
+ );
}
public function render(): string
{
- $gravatarUri = $this->uriFactory->createUri(
- 'https://gravatar.com/avatar/' . md5((string)$this->arguments['emailAddress']),
+ $emailHash = $this->normalizeEmailToHash(
+ (string)$this->arguments['emailAddress'],
);
- $queryArguments = [];
- if ($this->arguments['defaultImageUri'] !== null) {
- $queryArguments['d'] = urlencode($this->arguments['defaultImageUri']);
- }
+ $defaultImage = $this->normalizeDefaultImage(
+ (string)$this->arguments['defaultImage'],
+ );
- if ($this->arguments['size'] !== null) {
- $queryArguments['s'] = MathUtility::forceIntegerInRange((int)$this->arguments['size'], 1, 2048);
- }
+ $size = $this->normalizeSize(
+ (int)$this->arguments['size'],
+ );
$this->tag->addAttribute(
'src',
- (string)$gravatarUri->withQuery(HttpUtility::buildQueryString($queryArguments, '', true)),
+ $this->buildGravatarUri($emailHash, $defaultImage, $size),
);
return $this->tag->render();
}
+
+ private function normalizeEmailToHash(string $email): string
+ {
+ return md5(strtolower(trim($email)));
+ }
+
+ private function normalizeDefaultImage(string $default): string
+ {
+ $allowed = [
+ 'mp',
+ 'identicon',
+ 'retro',
+ 'monsterid',
+ 'wavatar',
+ 'robohash',
+ 'blank',
+ ];
+
+ return in_array($default, $allowed, true) ? $default : 'mp';
+ }
+
+ private function normalizeSize(int $size): int
+ {
+ return MathUtility::forceIntegerInRange($size, 1, 2048);
+ }
+
+ private function buildGravatarUri(string $hash, string $default, int $size): string
+ {
+ return (string)$this->uriFactory
+ ->createUri('https://www.gravatar.com/avatar/' . $hash)
+ ->withQuery(
+ HttpUtility::buildQueryString([
+ 'd' => $default,
+ 's' => $size,
+ ]),
+ );
+ }
}
diff --git a/Configuration/Sets/BlogExample/settings.definitions.yaml b/Configuration/Sets/BlogExample/settings.definitions.yaml
index ba77ec6..2080a3e 100644
--- a/Configuration/Sets/BlogExample/settings.definitions.yaml
+++ b/Configuration/Sets/BlogExample/settings.definitions.yaml
@@ -57,9 +57,17 @@ settings:
description: 'How many posts should be displayed per page of pagination?'
type: int
default: 3
- blogExample.defaultGravator:
- label: 'Default gravator icon'
+ blogExample.gravatarDefault:
+ label: 'Gravatar default image'
category: BlogExample.layout
- description: 'This icon is displayed for a comment if the email address of the commentary is not registered. '
+ description: 'Fallback image used by Gravatar when an email has no avatar.'
type: string
- default: 'EXT:blog_example/Resources/Public/Icons/default_gravatar.gif'
+ default: 'mp'
+ enum:
+ mp: 'Mystery Person (mp)'
+ identicon: 'Identicon'
+ retro: 'Retro'
+ monsterid: 'MonsterID'
+ wavatar: 'Wavatar'
+ robohash: 'Robohash'
+ blank: 'Blank'
diff --git a/Configuration/Sets/BlogExample/setup.typoscript b/Configuration/Sets/BlogExample/setup.typoscript
index 10f5538..7494179 100644
--- a/Configuration/Sets/BlogExample/setup.typoscript
+++ b/Configuration/Sets/BlogExample/setup.typoscript
@@ -3,7 +3,7 @@ plugin.tx_blogexample {
settings {
postsPerPage = {$blogExample.postsPerPage}
editorUsergroupUid = {$blogExample.editorUsergroupUid}
- defaultGravator = {$blogExample.defaultGravator}
+ gravatarDefault = {$blogExample.gravatarDefault}
}
persistence {
diff --git a/Resources/Private/Backend/Templates/ShowAllComments.html b/Resources/Private/Backend/Templates/ShowAllComments.html
index 83ee440..84e3bf2 100644
--- a/Resources/Private/Backend/Templates/ShowAllComments.html
+++ b/Resources/Private/Backend/Templates/ShowAllComments.html
@@ -14,7 +14,14 @@
Comments