You are here

class Robohash in Avatar Kit 8

Same name in this branch
  1. 8 avatars_robohash/src/Robohash.php \Drupal\avatars_robohash\Robohash
  2. 8 avatars_robohash/src/Plugin/AvatarGenerator/Robohash.php \Drupal\avatars_robohash\Plugin\AvatarGenerator\Robohash

Robohash avatar generator.

Plugin annotation


@AvatarGenerator(
  id = "robohash",
  label = @Translation("Robohash"),
  description = @Translation("Robots and monsters from Robohash.org"),
  fallback = TRUE,
  dynamic = FALSE,
  remote = TRUE
)

Hierarchy

Expanded class hierarchy of Robohash

2 string references to 'Robohash'
avatars_robohash.info.yml in avatars_robohash/avatars_robohash.info.yml
avatars_robohash/avatars_robohash.info.yml
avatars_robohash.schema.yml in avatars_robohash/config/schema/avatars_robohash.schema.yml
avatars_robohash/config/schema/avatars_robohash.schema.yml

File

avatars_robohash/src/Plugin/AvatarGenerator/Robohash.php, line 23

Namespace

Drupal\avatars_robohash\Plugin\AvatarGenerator
View source
class Robohash extends AvatarGeneratorBase {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'type' => '',
      'background' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function generateUri(AccountInterface $account) {
    $robohash = new RobohashAPI();
    $type_map = [
      'robots' => 'robot',
      'robot_heads' => 'robot_head',
      'monsters' => 'monster',
    ];
    $type = $this->configuration['type'];
    $background_map = [
      'transparent' => 'transparent',
      'background_1' => 'places',
      'background_2' => 'patterns',
    ];
    $background = $this->configuration['background'];
    return $robohash
      ->setIdentifier($this
      ->getIdentifier($account))
      ->setType(isset($type_map[$type]) ? $type_map[$type] : '')
      ->setBackground(isset($background_map[$background]) ? $background_map[$background] : '')
      ->getUrl();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['type'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Type'),
      '#options' => [
        'robots' => $this
          ->t('Robots'),
        'robot_heads' => $this
          ->t('Robot Heads'),
        'monsters' => $this
          ->t('Monsters'),
      ],
      '#default_value' => $this->configuration['type'],
    ];
    $form['background'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Background'),
      '#options' => [
        'transparent' => $this
          ->t('Transparent'),
        'background_1' => $this
          ->t('Places'),
        'background_2' => $this
          ->t('Patterns'),
      ],
      '#empty_value' => '',
      '#default_value' => $this->configuration['background'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['type'] = $form_state
      ->getValue('type');
    $this->configuration['background'] = $form_state
      ->getValue('background');
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    if (empty($this->configuration['type']) || empty($this->configuration['background'])) {
      $summary[] = $this
        ->t('Missing Configuration');
    }
    else {
      $summary[]['#markup'] = $this
        ->t('Type: @type | Background: @background', [
        '@type' => $this->configuration['type'],
        '@background' => $this->configuration['background'],
      ]);
    }
    return $summary;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvatarGeneratorBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
AvatarGeneratorBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
AvatarGeneratorBase::getFile public function Gets File object for an avatar. Overrides AvatarGeneratorPluginInterface::getFile 1
AvatarGeneratorBase::getIdentifier protected function Create a site-unique identifier for a user.
AvatarGeneratorBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
AvatarGeneratorBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
AvatarGeneratorBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
Robohash::buildConfigurationForm public function Form constructor. Overrides AvatarGeneratorBase::buildConfigurationForm
Robohash::defaultConfiguration public function Gets default configuration for this plugin. Overrides AvatarGeneratorBase::defaultConfiguration
Robohash::generateUri public function Creates a URI to an avatar. Overrides AvatarGeneratorBase::generateUri
Robohash::settingsSummary public function Generate a summary about the current configuration of the widget. Overrides AvatarGeneratorBase::settingsSummary
Robohash::submitConfigurationForm public function Form submission handler. Overrides AvatarGeneratorBase::submitConfigurationForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.