You are here

public function AvatarManager::refreshAvatarGenerator in Avatar Kit 8

Create avatar if it does not exist.

Parameters

\Drupal\user\UserInterface $user: A user entity.

\Drupal\avatars\AvatarGeneratorInterface $avatar_generator: An avatar generator instance.

int $scope: Caching scope level.

Return value

\Drupal\avatars\AvatarPreviewInterface|false An avatar preview entity.

Overrides AvatarManagerInterface::refreshAvatarGenerator

2 calls to AvatarManager::refreshAvatarGenerator()
AvatarManager::findValidAvatar in src/AvatarManager.php
Go down the the avatar generator preference hierarchy for a user.
AvatarManager::refreshAllAvatars in src/AvatarManager.php
Downloads all avatar previews for a user.

File

src/AvatarManager.php, line 146

Class

AvatarManager
Provides an avatar manager service.

Namespace

Drupal\avatars

Code

public function refreshAvatarGenerator(UserInterface $user, AvatarGeneratorInterface $avatar_generator, $scope) {
  if ($user
    ->isNew()) {
    return FALSE;
  }
  if ($avatar_preview = AvatarPreview::getAvatarPreview($avatar_generator, $user)) {

    // @todo fix this block. does not make much sense.
    if ($scope != AvatarPreviewInterface::SCOPE_TEMPORARY && $scope != $avatar_preview
      ->getScope()) {
      $avatar_preview
        ->setScope($scope)
        ->save();
    }
  }
  else {
    $file = $this
      ->getAvatarFile($avatar_generator, $user);
    $avatar_preview = AvatarPreview::create()
      ->setAvatarGeneratorId($avatar_generator
      ->id())
      ->setAvatar($file instanceof FileInterface ? $file : NULL)
      ->setUser($user)
      ->setScope($file instanceof FileInterface ? $scope : AvatarPreviewInterface::SCOPE_TEMPORARY);
    $avatar_preview
      ->save();
  }
  return $avatar_preview;
}