You are here

public function SubscriptionManager::subscribe in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Subscription/SubscriptionManager.php \Drupal\simplenews\Subscription\SubscriptionManager::subscribe()
  2. 3.x src/Subscription/SubscriptionManager.php \Drupal\simplenews\Subscription\SubscriptionManager::subscribe()

Subscribe a user to a newsletter or send a confirmation mail.

The $confirm parameter determines the action: FALSE = The user is subscribed TRUE = User receives an email to verify the address and complete the subscription A new subscription account is created when the user is subscribed to the first newsletter.

Parameters

string $mail: The email address to subscribe to the newsletter.

string $newsletter_id: The newsletter ID.

bool|null $confirm: TRUE = send confirmation mail; FALSE = subscribe immediate to the newsletter; NULL means the default from the chosen newsletter is used.

string $source: Indication for source of subscription. Simplenews uses these sources: website: via any website form (with or without confirmation email) mass subscribe: mass admin UI mass unsubscribe: mass admin UI action: Drupal actions

string $preferred_langcode: The language code (i.e. 'en', 'nl') of the user preferred language. Use '' for the site default language. Use NULL for the language of the current page.

Return value

$this

Overrides SubscriptionManagerInterface::subscribe

File

src/Subscription/SubscriptionManager.php, line 96

Class

SubscriptionManager
Default subscription manager.

Namespace

Drupal\simplenews\Subscription

Code

public function subscribe($mail, $newsletter_id, $confirm = NULL, $source = 'unknown', $preferred_langcode = NULL) {

  // Get current subscriptions if any.
  $subscriber = simplenews_subscriber_load_by_mail($mail);

  // If user is not subscribed to ANY newsletter, create a subscription account
  if (!$subscriber) {

    // To subscribe a user:
    //   - Fetch the users uid.
    //   - Determine the user preferred language.
    //   - Add the user to the database.
    //   - Get the full subscription object based on the mail address.
    // Note that step 3 gets subscription data based on mail address because the uid can be 0 (for anonymous users)
    $account = user_load_by_mail($mail);

    // If the site is multilingual:
    //  - Anonymous users are subscribed with their preferred language
    //    equal to the language of the current page.
    //  - Registered users will be subscribed with their default language as
    //    set in their account settings.
    // By default the preferred language is not set.
    if ($this->languageManager
      ->isMultilingual()) {
      if ($account) {
        $preferred_langcode = $account
          ->getPreferredLangcode();
      }
      else {
        $preferred_langcode = isset($preferred_langcode) ? $preferred_langcode : $this->languageManager
          ->getCurrentLanguage();
      }
    }
    else {
      $preferred_langcode = '';
    }
    $subscriber = Subscriber::create(array());
    $subscriber
      ->setMail($mail);
    if ($account) {
      $subscriber
        ->setUserId($account
        ->id());
    }
    $subscriber
      ->setLangcode($preferred_langcode);
    $subscriber
      ->setStatus(SubscriberInterface::ACTIVE);
    $subscriber
      ->save();
  }
  $newsletter = simplenews_newsletter_load($newsletter_id);

  // If confirmation is not explicitly specified, use the newsletter
  // configuration.
  if ($confirm === NULL) {
    $confirm = $this
      ->requiresConfirmation($newsletter, $subscriber
      ->getUserId());
  }
  if ($confirm) {

    // Create an unconfirmed subscription object if it doesn't exist yet.
    if (!$subscriber
      ->isSubscribed($newsletter_id)) {
      $subscriber
        ->subscribe($newsletter_id, SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED, $source);
      $subscriber
        ->save();
    }
    $this
      ->addConfirmation('subscribe', $subscriber, $newsletter);
  }
  elseif (!$subscriber
    ->isSubscribed($newsletter_id)) {

    // Subscribe the user if not already subscribed.
    $subscriber
      ->subscribe($newsletter_id, SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, $source);
    $subscriber
      ->save();
  }
  return $this;
}