You are here

class EntityAvatarIdentifier in Avatar Kit 8.2

An entity identifier.

Hierarchy

Expanded class hierarchy of EntityAvatarIdentifier

File

src/EntityAvatarIdentifier.php, line 15

Namespace

Drupal\avatars
View source
class EntityAvatarIdentifier extends AvatarIdentifier implements EntityAvatarIdentifierInterface {

  /**
   * An entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

  /**
   * {@inheritdoc}
   */
  public function getEntity() : EntityInterface {
    return $this->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function setEntity(EntityInterface $entity) : EntityAvatarIdentifierInterface {
    $this->entity = $entity;
    $raw = $this
      ->tokenReplace($entity);
    if (empty($raw)) {
      throw new AvatarKitEntityAvatarIdentifierException('Pre hashed string is empty after token replacement.');
    }
    $this
      ->setRaw($raw);
    return $this;
  }

  /**
   * Generate a pre-hashed string for an entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   Tokens will be replaced given this entity context.
   *
   * @return string
   *   A pre-hashed string for an entity.
   *
   * @throws \Drupal\avatars\Exception\AvatarKitEntityAvatarIdentifierException
   */
  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);
  }

  /**
   * Get the entity field handler service.
   *
   * @return \Drupal\avatars\AvatarKitEntityFieldHandlerInterface
   *   The entity field handler service.
   */
  protected function entityFieldHandler() : AvatarKitEntityFieldHandlerInterface {
    return \Drupal::service('avatars.entity.field_handler');
  }

  /**
   * Get Drupal placeholder/token replacement system.
   *
   * @return \Drupal\Core\Utility\Token
   *   The Drupal placeholder/token replacement system.
   */
  protected function token() : Token {
    return \Drupal::token();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityAvatarIdentifier::$entity protected property An entity.
EntityAvatarIdentifier::entityFieldHandler protected function Get the entity field handler service.
EntityAvatarIdentifier::getEntity public function Get the entity for this identifier. Overrides EntityAvatarIdentifierInterface::getEntity
EntityAvatarIdentifier::setEntity public function Set the entity for this identifier. Overrides EntityAvatarIdentifierInterface::setEntity
EntityAvatarIdentifier::token protected function Get Drupal placeholder/token replacement system.
EntityAvatarIdentifier::tokenReplace protected function Generate a pre-hashed string for an entity.