You are here

function avatars_entity_presave in Avatar Kit 8

Implements hook_entity_presave().

File

./avatars.module, line 95

Code

function avatars_entity_presave(EntityInterface $entity) {
  if ($entity instanceof UserInterface && !$entity
    ->isNew()) {

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

    // @todo: only notify if value of AK_FIELD_PICTURE_USER changed

    /** @var \Drupal\avatars\AvatarGeneratorStorageInterface $avatars_generator_storage */
    $avatars_generator_storage = \Drupal::entityTypeManager()
      ->getStorage('avatar_generator');
    foreach ($avatars_generator_storage
      ->getEnabledAvatarGenerators() as $avatar_generator) {
      if ($avatar_generator
        ->getPlugin()
        ->getPluginId() == 'user') {
        $avatar_manager
          ->notifyDynamicChange($avatar_generator, $entity);
      }
    }
  }
}