You are here

function avatars_entity_update in Avatar Kit 8

Same name and namespace in other branches
  1. 8.2 avatars.module \avatars_entity_update()

Implements hook_entity_update().

Dynamic avatar generators commonly rely on email to generate hash. If email changes, then destroy existing avatar previews from dynamic generators.

File

./avatars.module, line 118

Code

function avatars_entity_update(EntityInterface $entity) {
  if ($entity instanceof UserInterface && isset($entity->original)) {
    $original = $entity->original;
    if ($original
      ->getEmail() != $entity
      ->getEmail()) {

      /** @var \Drupal\avatars\AvatarManager $avatar_manager */
      $avatar_manager = \Drupal::service('avatars.avatar_manager');

      /** @var \Drupal\avatars\AvatarGeneratorStorageInterface $avatars_generator_storage */
      $avatars_generator_storage = \Drupal::entityTypeManager()
        ->getStorage('avatar_generator');
      foreach ($avatars_generator_storage
        ->getEnabledAvatarGenerators() as $avatar_generator) {
        $definition = $avatar_generator
          ->getPlugin()
          ->getPluginDefinition();
        if (!empty($definition['dynamic'])) {
          $avatar_manager
            ->notifyDynamicChange($avatar_generator, $entity);
        }
      }
    }
  }
}