You are here

public function Subscriber::subscribe in Simplenews 8.2

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

Add a subscription to a certain newsletter to the subscriber.

Parameters

string $newsletter_id: The ID of a newsletter.

int $status: The status of the subscription.

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

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

Overrides SubscriberInterface::subscribe

File

src/Entity/Subscriber.php, line 244

Class

Subscriber
Defines the simplenews subscriber entity.

Namespace

Drupal\simplenews\Entity

Code

public function subscribe($newsletter_id, $status = SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, $source = 'unknown', $timestamp = REQUEST_TIME) {
  if ($subscription = $this
    ->getSubscription($newsletter_id)) {
    $subscription->status = $status;
  }
  else {
    $data = [
      'target_id' => $newsletter_id,
      'status' => $status,
      'source' => $source,
      'timestamp' => $timestamp,
    ];
    $this->subscriptions
      ->appendItem($data);
  }
  if ($status == SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED) {
    \Drupal::moduleHandler()
      ->invokeAll('simplenews_subscribe', [
      $this,
      $newsletter_id,
    ]);
  }
}