You are here

public function Subscriber::unsubscribe in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Entity/Subscriber.php \Drupal\simplenews\Entity\Subscriber::unsubscribe()
  2. 3.x src/Entity/Subscriber.php \Drupal\simplenews\Entity\Subscriber::unsubscribe()

Delete a subscription to a certain newsletter of the subscriber.

Parameters

string $newsletter_id: The ID of a newsletter.

string $source: The source where the subscription comes from.

int $timestamp: The timestamp of when the subscription was added.

Overrides SubscriberInterface::unsubscribe

File

src/Entity/Subscriber.php, line 259

Class

Subscriber
Defines the simplenews subscriber entity.

Namespace

Drupal\simplenews\Entity

Code

public function unsubscribe($newsletter_id, $source = 'unknown', $timestamp = REQUEST_TIME) {
  if ($subscription = $this
    ->getSubscription($newsletter_id)) {
    $subscription->status = SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED;
  }
  else {
    $data = array(
      'target_id' => $newsletter_id,
      'status' => SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED,
      'source' => $source,
      'timestamp' => $timestamp,
    );
    $this->subscriptions
      ->appendItem($data);
  }

  // Clear eventually existing mail spool rows for this subscriber.
  \Drupal::service('simplenews.spool_storage')
    ->deleteMails(array(
    'snid' => $this
      ->id(),
    'newsletter_id' => $newsletter_id,
  ));
  \Drupal::moduleHandler()
    ->invokeAll('simplenews_unsubscribe', array(
    $this,
    $newsletter_id,
  ));
}