Gravatar.php in Avatar Kit 8
File
avatars_gravatar/src/Plugin/AvatarGenerator/Gravatar.php
View source
<?php
namespace Drupal\avatars_gravatar\Plugin\AvatarGenerator;
use Drupal\avatars\Plugin\AvatarGenerator\AvatarGeneratorBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\avatars_gravatar\Gravatar as GravatarAPI;
use Drupal\Core\Form\FormStateInterface;
class Gravatar extends AvatarGeneratorBase {
use StringTranslationTrait;
public function defaultConfiguration() {
return [
'maximum_rating' => '',
];
}
public function generateUri(AccountInterface $account) {
$gravatar = new GravatarAPI();
if (!empty($this->configuration['maximum_rating'])) {
$gravatar
->setRating($this->configuration['maximum_rating']);
}
return $gravatar
->setIdentifier($this
->getIdentifier($account))
->setType('gravatar')
->setFallbackType('404')
->setDimensions(256)
->getUrl();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['maximum_rating'] = [
'#type' => 'radios',
'#title' => $this
->t('Maximum content rating'),
'#options' => [
'' => $this
->t('Unrestricted'),
'g' => $this
->t('G: suitable for display on all websites with any audience type.'),
'pg' => $this
->t('PG: may contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence.'),
'r' => $this
->t('R: may contain such things as harsh profanity, intense violence, nudity, or hard drug use.'),
'x' => $this
->t('X: may contain hardcore sexual imagery or extremely disturbing violence.'),
],
'#required' => TRUE,
'#default_value' => $this->configuration['maximum_rating'],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['maximum_rating'] = $form_state
->getValue('maximum_rating');
}
public function settingsSummary() {
$summary = [];
if (!empty($this->configuration['maximum_rating'])) {
$summary[]['#markup'] = $this
->t('Maximum rating: @maximum_rating', [
'@maximum_rating' => $this->configuration['maximum_rating'],
]);
}
return $summary;
}
}