You are here

function simplenews_user_insert in Simplenews 8

Same name and namespace in other branches
  1. 8.2 simplenews.module \simplenews_user_insert()
  2. 7.2 simplenews.module \simplenews_user_insert()
  3. 7 simplenews.module \simplenews_user_insert()
  4. 3.x simplenews.module \simplenews_user_insert()

Implements hook_user_insert().

Update uid and preferred language when the new account was already subscribed.

File

./simplenews.module, line 433
Simplenews node handling, sent email, newsletter block and general hooks

Code

function simplenews_user_insert(UserInterface $account) {

  // Don't do anything if the user has no email.
  if (!$account
    ->getEmail()) {
    return;
  }

  // Use the email address to check if new account is already subscribed.
  $subscriber = simplenews_subscriber_load_by_mail($account
    ->getEmail());

  // If the user is subscribed, we update the subscriber with uid and language.
  if ($subscriber) {
    $subscriber
      ->setUserId($account
      ->id());
    $subscriber
      ->setLangcode($account
      ->getPreferredLangcode());

    // set inactive if not created by an administrator.
    if (!\Drupal::currentUser()
      ->hasPermission('administer users')) {

      // this user will be activated on first login (see simplenews_user_login)
      $subscriber
        ->setStatus(SubscriberInterface::INACTIVE);
    }
    else {
      $subscriber
        ->setStatus(SubscriberInterface::ACTIVE);
    }
    $subscriber
      ->save();
  }
}