D7SimplenewsSubscribers.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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",
  10. * source_module = "simplenews"
  11. * )
  12. */
  13. class D7SimplenewsSubscribers 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. ->orderBy('snid');
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function prepareRow(Row $row) {
  46. $result = parent::prepareRow($row);
  47. $version = $this->getModuleSchemaVersion('simplenews');
  48. $newsletter_id_field = 'newsletter_id';
  49. if ($version >= 7000 & $version < 7200) {
  50. $newsletter_id_field = 'tid';
  51. }
  52. // Add associated data from the subscriptions table.
  53. $q = $this->select('simplenews_subscription', 'sub');
  54. $q->addField('sub', $newsletter_id_field, 'newsletter_id');
  55. $q->fields('sub', ['status', 'timestamp', 'source']);
  56. $q->condition('sub.snid', $row->getSourceProperty('snid'));
  57. $subscriptions = $q->execute()->fetchAllAssoc('newsletter_id');
  58. $row->setSourceProperty('subscriptions', $subscriptions);
  59. return $result;
  60. }
  61. }