D6EdlpSons.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace Drupal\edlp_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\node\Plugin\migrate\source\d6\Node as D6Node;
  5. /**
  6. * Source plugin for edlp corpus migration.
  7. *
  8. * @MigrateSource(
  9. * id = "d6_edlp_sons"
  10. * )
  11. */
  12. class D6EdlpSons extends D6Node {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function query() {
  17. $query = $this->select('node_revisions', 'nr');
  18. $query->innerJoin('node', 'n', static::JOIN);
  19. $this->handleTranslations($query);
  20. $query->fields('n', [
  21. 'nid',
  22. 'type',
  23. 'language',
  24. 'status',
  25. 'created',
  26. 'changed',
  27. 'comment',
  28. 'promote',
  29. 'moderate',
  30. 'sticky',
  31. 'tnid',
  32. 'translate',
  33. ])
  34. ->fields('nr', [
  35. 'title',
  36. 'body',
  37. 'teaser',
  38. 'log',
  39. 'timestamp',
  40. 'format',
  41. 'vid',
  42. ]);
  43. $query->addField('n', 'uid', 'node_uid');
  44. $query->addField('nr', 'uid', 'revision_uid');
  45. // If the content_translation module is enabled, get the source langcode
  46. // to fill the content_translation_source field.
  47. if ($this->moduleHandler->moduleExists('content_translation')) {
  48. $query->leftJoin('node', 'nt', 'n.tnid = nt.nid');
  49. $query->addField('nt', 'language', 'source_langcode');
  50. }
  51. if (isset($this->configuration['node_type'])) {
  52. $query->condition('n.type', $this->configuration['node_type']);
  53. }
  54. return $query;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function fields() {
  60. $fields = [
  61. 'nid' => $this->t('Node ID'),
  62. 'type' => $this->t('Type'),
  63. 'title' => $this->t('Title'),
  64. 'body' => $this->t('Body'),
  65. 'format' => $this->t('Format'),
  66. 'teaser' => $this->t('Teaser'),
  67. 'node_uid' => $this->t('Node authored by (uid)'),
  68. 'revision_uid' => $this->t('Revision authored by (uid)'),
  69. 'created' => $this->t('Created timestamp'),
  70. 'changed' => $this->t('Modified timestamp'),
  71. 'status' => $this->t('Published'),
  72. 'promote' => $this->t('Promoted to front page'),
  73. 'sticky' => $this->t('Sticky at top of lists'),
  74. 'revision' => $this->t('Create new revision'),
  75. 'language' => $this->t('Language (fr, en, ...)'),
  76. 'tnid' => $this->t('The translation set id for this node'),
  77. 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
  78. ];
  79. return $fields;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function prepareRow(Row $row) {
  85. drush_print(' ');
  86. drush_print('- - - - - - - - - - - - - - - - - - - - - - - -');
  87. drush_print($row->getSourceProperty('title'));
  88. // format = 0 can happen when the body field is hidden. Set the format to 1
  89. // to avoid migration map issues (since the body field isn't used anyway).
  90. if ($row->getSourceProperty('format') === '0') {
  91. $row->setSourceProperty('format', $this->filterDefaultFormat);
  92. }
  93. if ($this->moduleExists('content') && $this->getModuleSchemaVersion('content') >= 6001) {
  94. foreach ($this->getFieldValues($row) as $field => $values) {
  95. $row->setSourceProperty($field, $values);
  96. }
  97. }
  98. $row_nid = $row->getSourceProperty('nid');
  99. // Make sure we always have a translation set.
  100. if ($row->getSourceProperty('tnid') == 0) {
  101. $row->setSourceProperty('tnid', $row_nid);
  102. }
  103. // Genres
  104. $genres = [];
  105. foreach ($row->getSourceProperty('field_genre') as $key => $value) {
  106. $genres[] = array($value['value']);
  107. }
  108. $row->setSourceProperty('genres', $genres);
  109. // locuteurs
  110. $locuteurs = [];
  111. foreach ($row->getSourceProperty('field_artiste') as $key => $value) {
  112. $term_name = $value['value'];
  113. // check if term already exists in configured vocabulary
  114. $term = array_shift(taxonomy_term_load_multiple_by_name($term_name, 'locuteurs'));
  115. if(!$term){
  116. // if not create the term and get the tid
  117. $term = \Drupal\taxonomy\Entity\Term::create([
  118. 'vid' => 'locuteurs',
  119. 'name' => $term_name,
  120. ]);
  121. $term->save();
  122. }
  123. // record the tid in array
  124. $locuteurs[] = $term->get('tid')->value;
  125. }
  126. $row->setSourceProperty('locuteurs', $locuteurs);
  127. // collectionneurs
  128. $collectionneurs = [];
  129. foreach ($row->getSourceProperty('field_compositeur') as $key => $value) {
  130. $term_name = $value['value'];
  131. // check if term already exists in configured vocabulary
  132. $term = array_shift(taxonomy_term_load_multiple_by_name($term_name, 'collectionneurs'));
  133. if(!$term){
  134. // if not create the term and get the tid
  135. $term = \Drupal\taxonomy\Entity\Term::create([
  136. 'vid' => 'collectionneurs',
  137. 'name' => $term_name,
  138. ]);
  139. $term->save();
  140. }
  141. // record the tid in array
  142. $collectionneurs[] = $term->get('tid')->value;
  143. }
  144. $row->setSourceProperty('collectionneurs', $collectionneurs);
  145. // son
  146. // drush_print('field_mp3 : ', 0, null, false);
  147. // drush_print_r($row->getSourceProperty('field_mp3'));
  148. $sons = [];
  149. foreach ($row->getSourceProperty('field_mp3') as $key => $value) {
  150. $sons[] = array($value['fid']);
  151. }
  152. $row->setSourceProperty('sons_fid', $sons);
  153. // get the workflow state (will be converted on status)
  154. $query = $this->select('workflow_node', 'wn');
  155. $query->fields('wn', ['sid']);
  156. $query->condition('wn.nid', $row_nid);
  157. $results = $query->execute()->fetchField();
  158. // drush_print('workflow query results : ', 0, null, false);
  159. // drush_print_r($results);
  160. $row->setSourceProperty('workflow', $results);
  161. $row = parent::prepareRow($row);
  162. return $row;
  163. }
  164. }