You are here

public function AvatarKitLocalCache::cacheLocalFileEntity in Avatar Kit 8.2

Creates an avatar cache entity by linking a pre-existing file entity.

Parameters

string $service_id: An avatar service ID.

string $uri: The URL of an file stored locally.

\Drupal\avatars\EntityAvatarIdentifierInterface $identifier: An entity avatar identifier.

Return value

\Drupal\avatars\Entity\AvatarCacheInterface|null An avatar cache entity, or NULL if no pre-existing file entity exists with provided URI.

Overrides AvatarKitLocalCacheInterface::cacheLocalFileEntity

File

src/AvatarKitLocalCache.php, line 79

Class

AvatarKitLocalCache
Cache remote files locally into file entities.

Namespace

Drupal\avatars

Code

public function cacheLocalFileEntity(string $service_id, string $uri, EntityAvatarIdentifierInterface $identifier) : ?AvatarCacheInterface {
  $identifier_hash = $identifier
    ->getHashed();
  $files = $this->fileStorage
    ->loadByProperties([
    'uri' => $uri,
  ]);
  if (!$files) {
    return NULL;
  }
  $file = reset($files);

  /** @var \Drupal\avatars\Entity\AvatarCacheInterface $avatar_cache */
  $avatar_cache = $this->avatarCacheStorage
    ->create([
    'avatar_service' => $service_id,
    'identifier' => $identifier_hash,
    'avatar' => $file,
  ]);
  $avatar_cache
    ->save();
  return $avatar_cache;
}