You are here

public function SpoolStorage::addFromEntity in Simplenews 8

Add the newsletter node to the mail spool.

The caller is responsible for saving the changed node entity.

Parameters

\Drupal\node\NodeInterface $node: The newsletter node to be sent.

Overrides SpoolStorageInterface::addFromEntity

File

src/Spool/SpoolStorage.php, line 234

Class

SpoolStorage
Default database spool storage.

Namespace

Drupal\simplenews\Spool

Code

public function addFromEntity(NodeInterface $node) {
  $newsletter = $node->simplenews_issue->entity;
  $handler = $node->simplenews_issue->handler;
  $handler_settings = $node->simplenews_issue->handler_settings;
  $recipient_handler = simplenews_get_recipient_handler($newsletter, $handler, $handler_settings);

  // To send the newsletter, the node id and target email addresses
  // are stored in the spool.
  // Only subscribed recipients are stored in the spool (status = 1).
  $select = $recipient_handler
    ->buildRecipientQuery();
  $select
    ->addExpression('\'node\'', 'entity_type');
  $select
    ->addExpression($node
    ->id(), 'entity_id');
  $select
    ->addExpression(SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, 'status');
  $select
    ->addExpression(REQUEST_TIME, 'timestamp');
  $node->simplenews_issue->subscribers = simplenews_count_subscriptions($node->simplenews_issue->target_id);
  $this->connection
    ->insert('simplenews_mail_spool')
    ->from($select)
    ->execute();

  // Update simplenews newsletter status to send pending.
  $node->simplenews_issue->status = SIMPLENEWS_STATUS_SEND_PENDING;

  // Notify other modules that a newsletter was just spooled.
  $this->moduleHandler
    ->invokeAll('simplenews_spooled', array(
    $node,
  ));
}