D7SimplenewsSubscribersNotUser.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
  5. /**
  6. * Migration source for Subscriber entries in D7.
  7. *
  8. * @MigrateSource(
  9. * id = "d7_simplenews_subscribers_notuser",
  10. * source_module = "simplenews"
  11. * )
  12. */
  13. class D7SimplenewsSubscribersNotUser extends DrupalSqlBase {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function fields() {
  18. return [
  19. 'snid' => $this->t('Subscriber ID'),
  20. 'activated' => $this->t('Activated'),
  21. 'mail' => $this->t("Subscriber's e-mail address"),
  22. 'uid' => $this->t('Corresponding user'),
  23. 'language' => $this->t('Language'),
  24. 'changes' => $this->t('Pending unconfirmed subscription changes'),
  25. 'created' => $this->t('Time of creation'),
  26. ];
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getIds() {
  32. return ['snid' => ['type' => 'integer']];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function query() {
  38. return $this->select('simplenews_subscriber', 's')
  39. ->fields('s')
  40. ->condition('s.uid', 0)
  41. ->orderBy('snid');
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function prepareRow(Row $row) {
  47. $result = parent::prepareRow($row);
  48. $version = $this->getModuleSchemaVersion('simplenews');
  49. $newsletter_id_field = 'newsletter_id';
  50. if ($version >= 7000 & $version < 7200) {
  51. $newsletter_id_field = 'tid';
  52. }
  53. // Add associated data from the subscriptions table.
  54. $q = $this->select('simplenews_subscription', 'sub');
  55. $q->addField('sub', $newsletter_id_field, 'newsletter_id');
  56. $q->fields('sub', ['status', 'timestamp', 'source']);
  57. $q->condition('sub.snid', $row->getSourceProperty('snid'));
  58. $subscriptions = $q->execute()->fetchAllAssoc('newsletter_id');
  59. $row->setSourceProperty('subscriptions', $subscriptions);
  60. return $result;
  61. }
  62. }