You are here

class LetterAvatar in Letter Avatar 8

Gravatar avatar generator.

Plugin annotation


@AvatarGenerator(
  id = "letter_avatar",
  label = @Translation("Letter Avatar"),
  description = @Translation("Letter generated by the LetterAvatar library."),
  fallback = TRUE,
  dynamic = FALSE,
  remote = TRUE
)

Hierarchy

Expanded class hierarchy of LetterAvatar

File

src/Plugin/AvatarGenerator/LetterAvatar.php, line 27
Contains \Drupal\letter_avatar\Plugin\AvatarGenerator\LetterAvatar.

Namespace

Drupal\letter_avatar\Plugin\AvatarGenerator
View source
class LetterAvatar extends AvatarGeneratorBase {

  /**
   * {@inheritdoc}
   */
  public function getFile(AccountInterface $account) {
    $directory = 'public://avatar_kit/ak_letter';
    if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {

      /** @var \Drupal\Core\File\FileSystemInterface $file_system */
      $file_system = \Drupal::service('file_system');
      $letter = mb_substr($account
        ->getUsername(), 0, 1);
      $path = $directory . '/' . $account
        ->id() . '.png';

      // todo: update existing file entity
      // if you update a file on the file system directly, page caches and image
      // styles will not flush.
      $ids = \Drupal::entityManager()
        ->getStorage('file')
        ->getQuery()
        ->condition('uri', $path)
        ->execute();
      if ($id = reset($ids)) {
        $file = File::load($id);
        $file
          ->delete();
      }
      $letterAvatar = new LetterAvatarAPI();
      $letterAvatar
        ->generate($letter, 256)
        ->saveAsPng($file_system
        ->realpath($path));

      // File cannot chain methods.
      $file = File::create();
      $file
        ->setFileUri($path);
      $file
        ->setOwnerId($account
        ->id());

      // Temporary until AvatarPreview adds usage.
      $file
        ->setTemporary();
      return $file;
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvatarGeneratorBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 3
AvatarGeneratorBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
AvatarGeneratorBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 3
AvatarGeneratorBase::generateUri public function Creates a URI to an avatar. Overrides AvatarGeneratorPluginInterface::generateUri 8
AvatarGeneratorBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
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::settingsSummary public function Generate a summary about the current configuration of the widget. Overrides AvatarGeneratorPluginInterface::settingsSummary 3
AvatarGeneratorBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 3
AvatarGeneratorBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
AvatarGeneratorBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
LetterAvatar::getFile public function Gets File object for an avatar. Overrides AvatarGeneratorBase::getFile
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.