You are here

protected function EntityAvatarIdentifier::tokenReplace in Avatar Kit 8.2

Generate a pre-hashed string for an entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Tokens will be replaced given this entity context.

Return value

string A pre-hashed string for an entity.

Throws

\Drupal\avatars\Exception\AvatarKitEntityAvatarIdentifierException

1 call to EntityAvatarIdentifier::tokenReplace()
EntityAvatarIdentifier::setEntity in src/EntityAvatarIdentifier.php
Set the entity for this identifier.

File

src/EntityAvatarIdentifier.php, line 58

Class

EntityAvatarIdentifier
An entity identifier.

Namespace

Drupal\avatars

Code

protected function tokenReplace(EntityInterface $entity) : string {
  $field_config = $this
    ->entityFieldHandler()
    ->getAvatarFieldConfig($entity);
  if (!$field_config) {
    throw new AvatarKitEntityAvatarIdentifierException('No field mapping/field config for this entity.');
  }
  $hash_settings = $field_config
    ->getThirdPartySetting('avatars', 'hash');
  $token_text = $hash_settings['contents'] ?? '';
  if (empty($token_text)) {
    throw new AvatarKitEntityAvatarIdentifierException('No token text defined for this entity.');
  }
  $data = [];
  $data[$entity
    ->getEntityTypeId()] = $entity;
  $options = [];
  $options['clear'] = TRUE;
  return $this
    ->token()
    ->replace($token_text, $data, $options);
}