D7SimplenewsNodes.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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->addField('n', 'uid', 'node_uid');
  80. $query->addField('nr', 'uid', 'revision_uid');
  81. $query->innerJoin('node', 'n', static::JOIN);
  82. if (isset($this->configuration['node_type'])) {
  83. $query->condition('n.type', $this->configuration['node_type']);
  84. }
  85. return $query;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function prepareRow(Row $row) {
  91. $nid = $row->getSourceProperty('nid');
  92. $vid = $row->getSourceProperty('vid');
  93. $type = $row->getSourceProperty('type');
  94. $title = $row->getSourceProperty('title');
  95. // drush_print('-- '.$nid."\t".$title);
  96. // Get Field API field values.
  97. foreach ($this->getFields('node', $type) as $field_name => $field) {
  98. $row->setSourceProperty($field_name, $this->getFieldValues('node', $field_name, $nid, $vid, 'und'));
  99. }
  100. // workflow
  101. $query = $this->select('workflow_node', 'wn');
  102. $query->fields('wn', ['sid']);
  103. $query->condition('wn.nid', $nid);
  104. $results = $query->execute()->fetchField();
  105. if(!$results){
  106. $results = 2;
  107. drush_print('WARNING: no workflow');
  108. }
  109. $row->setSourceProperty('workflow', $results);
  110. return parent::prepareRow($row);
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function fields() {
  116. $fields = [
  117. 'nid' => $this->t('Node ID'),
  118. 'type' => $this->t('Type'),
  119. 'title' => $this->t('Title'),
  120. 'node_uid' => $this->t('Node authored by (uid)'),
  121. 'revision_uid' => $this->t('Revision authored by (uid)'),
  122. 'created' => $this->t('Created timestamp'),
  123. 'changed' => $this->t('Modified timestamp'),
  124. 'status' => $this->t('Published'),
  125. 'promote' => $this->t('Promoted to front page'),
  126. 'sticky' => $this->t('Sticky at top of lists'),
  127. 'revision' => $this->t('Create new revision'),
  128. 'language' => $this->t('Language (fr, en, ...)'),
  129. 'tnid' => $this->t('The translation set id for this node'),
  130. 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
  131. ];
  132. return $fields;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function getIds() {
  138. $ids['nid']['type'] = 'integer';
  139. $ids['nid']['alias'] = 'n';
  140. return $ids;
  141. }
  142. }