SendIssue.php in Simplenews 8
File
src/Plugin/Action/SendIssue.php
View source
<?php
namespace Drupal\simplenews\Plugin\Action;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
class SendIssue extends ActionBase {
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 (!empty($nodes)) {
$conditions = array(
'entity_id' => array_keys($nodes),
'entity_type' => 'node',
);
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);
}
}
}
public function execute($node = NULL) {
$this
->executeMultiple(array(
$node,
));
}
public function access($node, AccountInterface $account = NULL, $return_as_object = FALSE) {
if ($node
->hasField('simplenews_issue') && $node->simplenews_issue->target_id != NULL) {
return AccessResult::allowedIfHasPermission($account, 'administer newsletters')
->orIf(AccessResult::allowedIfHasPermission($account, 'send newsletter'));
}
return AccessResult::neutral();
}
}