You are here

public function SendIssue::executeMultiple in Simplenews 8

Executes the plugin for an array of objects.

Parameters

array $objects: An array of entities.

Overrides ActionBase::executeMultiple

1 call to SendIssue::executeMultiple()
SendIssue::execute in src/Plugin/Action/SendIssue.php
Executes the plugin.

File

src/Plugin/Action/SendIssue.php, line 23

Class

SendIssue
Sends a newsletter issue.

Namespace

Drupal\simplenews\Plugin\Action

Code

public function executeMultiple(array $entities) {
  $nodes = array();
  $labels = array();
  foreach ($entities as $node) {
    if ($node->simplenews_issue->status != SIMPLENEWS_STATUS_SEND_NOT) {
      continue;
    }
    if (!$node
      ->isPublished()) {
      simplenews_issue_update_sent_status($node, SIMPLENEWS_COMMAND_SEND_PUBLISH);
      $this
        ->messenger()
        ->addMessage(t('Newsletter issue %title is unpublished and will be sent on publish.', array(
        '%title' => $node
          ->label(),
      )));
      continue;
    }
    \Drupal::service('simplenews.spool_storage')
      ->addFromEntity($node);
    $nodes[$node
      ->id()] = $node;
    $labels[$node
      ->id()] = $node
      ->label();
  }

  // If there were any newsletters sent, display a message.
  if (!empty($nodes)) {
    $conditions = array(
      'entity_id' => array_keys($nodes),
      'entity_type' => 'node',
    );

    // Attempt to send immediately, if configured to do so.
    if (\Drupal::service('simplenews.mailer')
      ->attemptImmediateSend($conditions)) {
      $this
        ->messenger()
        ->addMessage(t('Sent the following newsletter(s): %titles.', array(
        '%titles' => implode(', ', $labels),
      )));
      $status = SIMPLENEWS_STATUS_SEND_READY;
    }
    else {
      $this
        ->messenger()
        ->addMessage(t('The following newsletter(s) are now pending: %titles.', array(
        '%titles' => implode(', ', $labels),
      )));
      $status = SIMPLENEWS_STATUS_SEND_PENDING;
    }
    foreach ($nodes as $node) {
      simplenews_issue_update_sent_status($node, $status);
    }
  }
}