Commit e5509153 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3057125 by AdamPS, jonathanshaw: Create a temporary subscriber for...

Issue #3057125 by AdamPS, jonathanshaw: Create a temporary subscriber for recipient that is User entity
parent ecc884cb
Loading
Loading
Loading
Loading
+4 −3
Changes for modules/simplenews_demo/src/Plugin/simplenews/RecipientHandler/RecipientHandlerNewUsers.php: 4 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -18,9 +18,10 @@ class RecipientHandlerNewUsers extends RecipientHandlerEntityBase {
   * {@inheritdoc}
   */
  protected function buildEntityQuery() {
    return \Drupal::entityQuery('simplenews_subscriber')
      ->condition('uid.entity.access', 0)
      ->condition('uid.entity.status', TRUE);
    return \Drupal::entityQuery('user')
      ->exists('mail')
      ->condition('access', 0)
      ->condition('status', TRUE);
  }

  /**
+1 −1
Changes for modules/simplenews_demo/src/Plugin/simplenews/RecipientHandler/RecipientHandlerSiteMail.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ class RecipientHandlerSiteMail extends RecipientHandlerBase {
   */
  public function addToSpool() {
    $subscriber_data = ['mail' => \Drupal::config('system.site')->get('mail')];
    $this->addArrayToSpool('data', [serialize($subscriber_data)]);
    $this->addArrayToSpool('data', [$subscriber_data]);
    return 1;
  }

+9 −3
Changes for src/Plugin/simplenews/RecipientHandler/RecipientHandlerBase.php: 9 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -126,13 +126,14 @@ abstract class RecipientHandlerBase extends PluginBase implements RecipientHandl
   * newsletter.
   *
   * @param string $field
   *   Field to set: snid or data
   *   Field to set: 'snid', 'data' (automatically serialised) or 'uid'
   *  (automatically stored in 'data' array with key 'uid').
   * @param array $values
   *   Values to set for field.
   */
  protected function addArrayToSpool($field, $values) {
    if (empty($values)) {
      return 0;
      return;
    }

    $template = [
@@ -143,12 +144,17 @@ abstract class RecipientHandlerBase extends PluginBase implements RecipientHandl
      'newsletter_id' => $this->getNewsletterId(),
    ];

    if ($field == 'uid') {
      $field = 'data';
      $values = array_map(function($v) { return ['uid' => $v]; }, $values);
    }

    $insert = $this->connection->insert('simplenews_mail_spool')
      ->fields(array_merge(array_keys($template), [$field]));

    foreach ($values as $value) {
      $row = $template;
      $row[$field] = $value;
      $row[$field] = ($field == 'data') ? serialize($value) : $value;
      $insert->values($row);
    }

+5 −3
Changes for src/Plugin/simplenews/RecipientHandler/RecipientHandlerEntityBase.php: 5 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -11,8 +11,10 @@ abstract class RecipientHandlerEntityBase extends RecipientHandlerBase {
   * {@inheritdoc}
   */
  public function addToSpool() {
    $ids = $this->buildEntityQuery()->execute();
    $this->addArrayToSpool('snid', $ids);
    $query = $this->buildEntityQuery();
    $ids = $query->execute();
    $field = ($query->getEntityTypeId() == 'user') ? 'uid' : 'snid';
    $this->addArrayToSpool($field, $ids);
    return count($ids);
  }

@@ -27,7 +29,7 @@ abstract class RecipientHandlerEntityBase extends RecipientHandlerBase {
   * Build the query that gets the list of subscribers.
   *
   * @return \Drupal\Core\Entity\Query\QueryInterface
   *   Entity query on 'simplenews_subscriber'.
   *   Entity query on 'simplenews_subscriber' or 'user'.
   */
  abstract protected function buildEntityQuery();

+3 −3
Changes for src/Spool/SpoolList.php: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -75,9 +75,9 @@ class SpoolList implements SpoolListInterface {
      $subscriber = Subscriber::load($spool_data->snid);
    }

    if (!$subscriber) {
      // If loading the subscriber failed, set the processed status done and
      // proceed with the next mail.
    if (!$subscriber || !$subscriber->getMail()) {
      // If loading the subscriber failed or no email is available, set the
      // processed status done and proceed with the next mail.
      $this->processed[$spool_data->msid]->result = array(
        'status' => SpoolStorageInterface::STATUS_DONE,
        'error' => TRUE