GravatarGenerator.php in Avatar Kit 8
File
avatars_gravatar/src/Plugin/AvatarGenerator/GravatarGenerator.php
View source
<?php
namespace Drupal\avatars_gravatar\Plugin\AvatarGenerator;
use Drupal\avatars\Plugin\AvatarGenerator\AvatarGeneratorBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\avatars_gravatar\Gravatar as GravatarAPI;
class GravatarGenerator extends AvatarGeneratorBase {
use StringTranslationTrait;
public function defaultConfiguration() {
return [
'type' => '',
];
}
public function generateUri(AccountInterface $account) {
$gravatar = new GravatarAPI();
return $gravatar
->setIdentifier($this
->getIdentifier($account))
->setType($this->configuration['type'])
->setDimensions(256)
->getUrl();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['type'] = [
'#type' => 'radios',
'#title' => $this
->t('Type'),
'#options' => [
'identicon' => $this
->t('Identicon'),
'monsterid' => $this
->t('Monster ID'),
'wavatar' => $this
->t('Wavatar'),
'retro' => $this
->t('8-bit style avatar'),
],
'#default_value' => $this->configuration['type'],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['type'] = $form_state
->getValue('type');
}
public function settingsSummary() {
$summary = [];
if (empty($this->configuration['type'])) {
$summary[] = $this
->t('Missing Configuration');
}
else {
$summary[]['#markup'] = $this
->t('Type: @type', [
'@type' => $this->configuration['type'],
]);
}
return $summary;
}
}