D7SimplenewsNodes.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\Core\Extension\ModuleHandlerInterface;
  4. use Drupal\migrate\Row;
  5. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  6. use Drupal\Core\Database\Query\SelectInterface;
  7. use Drupal\Core\Entity\EntityManagerInterface;
  8. use Drupal\Core\Extension\ModuleHandler;
  9. use Drupal\Core\State\StateInterface;
  10. use Drupal\migrate\Plugin\MigrationInterface;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * Drupal 7 node source from database.
  14. *
  15. * @MigrateSource(
  16. * id = "d7_simplenews_nodes",
  17. * source_module = "node"
  18. * )
  19. */
  20. class D7SimplenewsNodes extends FieldableEntity {
  21. /**
  22. * The module handler.
  23. *
  24. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  25. */
  26. protected $moduleHandler;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) {
  31. parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
  32. $this->moduleHandler = $module_handler;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
  38. return new static(
  39. $configuration,
  40. $plugin_id,
  41. $plugin_definition,
  42. $migration,
  43. $container->get('state'),
  44. $container->get('entity.manager'),
  45. $container->get('module_handler')
  46. );
  47. }
  48. /**
  49. * The join options between the node and the node_revisions table.
  50. */
  51. const JOIN = 'n.vid = nr.vid';
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function query() {
  56. // Select node in its last revision.
  57. $query = $this->select('node_revision', 'nr')
  58. ->fields('n', [
  59. 'nid',
  60. 'type',
  61. 'uid',
  62. 'language',
  63. 'status',
  64. 'created',
  65. 'changed',
  66. 'comment',
  67. 'promote',
  68. 'sticky',
  69. 'tnid',
  70. 'translate',
  71. ])
  72. ->fields('nr', [
  73. 'vid',
  74. 'title',
  75. 'log',
  76. 'timestamp',
  77. ])
  78. ->orderBy('changed');
  79. $query->innerJoin('node', 'n', static::JOIN);
  80. $query->addField('n', 'uid', 'node_uid');
  81. $query->addField('nr', 'uid', 'revision_uid');
  82. $query->innerJoin('simplenews_newsletter', 'snn', 'snn.nid = n.nid');
  83. $query->addField('snn', 'status', 'snn_status');
  84. $query->addField('snn', 'sent_subscriber_count', 'snn_sent_subscriber_count');
  85. if (isset($this->configuration['node_type'])) {
  86. $query->condition('n.type', $this->configuration['node_type']);
  87. }
  88. return $query;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function prepareRow(Row $row) {
  94. $nid = $row->getSourceProperty('nid');
  95. $vid = $row->getSourceProperty('vid');
  96. $type = $row->getSourceProperty('type');
  97. $title = $row->getSourceProperty('title');
  98. // drush_print('-- '.$nid."\t".$title);
  99. // Get Field API field values.
  100. foreach ($this->getFields('node', $type) as $field_name => $field) {
  101. $row->setSourceProperty($field_name, $this->getFieldValues('node', $field_name, $nid, $vid, 'und'));
  102. }
  103. // workflow
  104. $query = $this->select('workflow_node', 'wn');
  105. $query->fields('wn', ['sid']);
  106. $query->condition('wn.nid', $nid);
  107. $results = $query->execute()->fetchField();
  108. if(!$results){
  109. $results = 2;
  110. drush_print('WARNING: no workflow');
  111. }
  112. $row->setSourceProperty('workflow', $results);
  113. return parent::prepareRow($row);
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function fields() {
  119. $fields = [
  120. 'nid' => $this->t('Node ID'),
  121. 'type' => $this->t('Type'),
  122. 'title' => $this->t('Title'),
  123. 'node_uid' => $this->t('Node authored by (uid)'),
  124. 'revision_uid' => $this->t('Revision authored by (uid)'),
  125. 'created' => $this->t('Created timestamp'),
  126. 'changed' => $this->t('Modified timestamp'),
  127. 'status' => $this->t('Published'),
  128. 'promote' => $this->t('Promoted to front page'),
  129. 'sticky' => $this->t('Sticky at top of lists'),
  130. 'revision' => $this->t('Create new revision'),
  131. 'language' => $this->t('Language (fr, en, ...)'),
  132. 'tnid' => $this->t('The translation set id for this node'),
  133. 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
  134. ];
  135. return $fields;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getIds() {
  141. $ids['nid']['type'] = 'integer';
  142. $ids['nid']['alias'] = 'n';
  143. return $ids;
  144. }
  145. }