You are here

public function Subscriber::subscribe in Simplenews 8

Same name and namespace in other branches
  1. 8.2 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 238

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 = array(
      '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', array(
      $this,
      $newsletter_id,
    ));
  }
}