public function SubscriptionManager::subscribe in Simplenews 8.2
Same name and namespace in other branches
- 8 src/Subscription/SubscriptionManager.php \Drupal\simplenews\Subscription\SubscriptionManager::subscribe()
- 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 121
Class
- SubscriptionManager
- Default subscription manager.
Namespace
Drupal\simplenews\SubscriptionCode
public function subscribe($mail, $newsletter_id, $confirm = NULL, $source = 'unknown', $preferred_langcode = NULL) {
// Get/create subscriber entity.
$preferred_langcode = $preferred_langcode ?? $this->languageManager
->getCurrentLanguage();
$subscriber = Subscriber::loadByMail($mail, 'create', $preferred_langcode);
$newsletter = 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;
}