public function SubscriptionManager::unsubscribe in Simplenews 8.2
Same name and namespace in other branches
- 8 src/Subscription/SubscriptionManager.php \Drupal\simplenews\Subscription\SubscriptionManager::unsubscribe()
- 3.x src/Subscription/SubscriptionManager.php \Drupal\simplenews\Subscription\SubscriptionManager::unsubscribe()
Unsubscribe a user from a mailing list or send a confirmation mail.
The $confirm parameter determines the action: FALSE = The user is unsubscribed TRUE = User receives an email to verify the address and complete the subscription cancellation.
Parameters
string $mail: The email address to unsubscribe from the mailing list.
string $newsletter_id: The newsletter ID.
bool|null $confirm: If TRUE, send a confirmation mail; if FALSE, unsubscribe immediately. NULL means the default from the chosen newsletter is used.
string $source: Indicates the unsubscribe source. 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.
Return value
$this
Overrides SubscriptionManagerInterface::unsubscribe
File
- src/
Subscription/ SubscriptionManager.php, line 153
Class
- SubscriptionManager
- Default subscription manager.
Namespace
Drupal\simplenews\SubscriptionCode
public function unsubscribe($mail, $newsletter_id, $confirm = NULL, $source = 'unknown') {
$subscriber = Subscriber::loadByMail($mail);
if (!$subscriber) {
throw new \Exception('The subscriber does not exist.');
}
// The unlikely case that a user is unsubscribed from a non existing mailing
// list is logged.
if (!($newsletter = Newsletter::load($newsletter_id))) {
$this->logger
->error('Attempt to unsubscribe from non existing mailing list ID %id', [
'%id' => $newsletter_id,
]);
return $this;
}
// If confirmation is not explicitly specified, use the newsletter
// configuration.
if ($confirm === NULL) {
$confirm = $this
->requiresConfirmation($newsletter, $subscriber
->getUserId());
}
if ($confirm) {
$this
->addConfirmation('unsubscribe', $subscriber, $newsletter);
}
elseif ($subscriber
->isSubscribed($newsletter_id)) {
// Unsubscribe the user from the mailing list.
$subscriber
->unsubscribe($newsletter_id, $source);
$subscriber
->save();
}
return $this;
}