D6EdlpCorpus.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_corpus"
  10. * )
  11. */
  12. class D6EdlpCorpus 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. // get the article content
  104. // drush_print_r($row->getSourceProperty('field_article'));
  105. $article_nid = $row->getSourceProperty('field_article')[0]['nid'];
  106. if($article_nid){
  107. // drush_print($article_nid);
  108. $query = $this->select('content_type_episode', 'ctp');
  109. // // $query->fields('cte', ['field_entree_value']);
  110. $query->condition('ctp.nid', $article_nid);
  111. $query->join('node_revisions', 'nr', 'nr.vid = ctp.vid');
  112. $query->addField('nr', 'body');
  113. $article = $query->execute()
  114. ->fetchField();
  115. // drush_print_r($article);
  116. $row->setSourceProperty('article', $article);
  117. }
  118. // get the entree values
  119. $query = $this->select('term_node', 'tn');
  120. // $query->fields('cte', ['field_entree_value']);
  121. $query->condition('tn.nid', $row_nid);
  122. $query->join('term_data', 'td', 'td.tid = tn.tid');
  123. $query->condition('td.vid', 2); // vid of voc entrees
  124. $query->addField('tn', 'tid');
  125. $query->distinct();
  126. $results = $query->execute()->fetchAll();
  127. $entrees = [];
  128. foreach ($results as $key => $value) {
  129. $entrees[] = array($value['tid']);
  130. }
  131. $row->setSourceProperty('entrees', $entrees);
  132. // Genres
  133. $genres = [];
  134. foreach ($row->getSourceProperty('field_genre') as $key => $value) {
  135. $genres[] = array($value['value']);
  136. }
  137. $row->setSourceProperty('genres', $genres);
  138. $langues = [];
  139. foreach ($row->getSourceProperty('field_langue') as $key => $value) {
  140. $langues[] = array($value['value']);
  141. }
  142. $row->setSourceProperty('langues', $langues);
  143. // locuteurs
  144. $locuteurs = [];
  145. foreach ($row->getSourceProperty('field_artiste') as $key => $value) {
  146. $term_name = $value['value'];
  147. // check if term already exists in configured vocabulary
  148. $term = array_shift(taxonomy_term_load_multiple_by_name($term_name, 'locuteurs'));
  149. if(!$term){
  150. // if not create the term and get the tid
  151. $term = \Drupal\taxonomy\Entity\Term::create([
  152. 'vid' => 'locuteurs',
  153. 'name' => $term_name,
  154. ]);
  155. $term->save();
  156. }
  157. // record the tid in array
  158. $locuteurs[] = $term->get('tid')->value;
  159. }
  160. $row->setSourceProperty('locuteurs', $locuteurs);
  161. // collectionneurs
  162. $collectionneurs = [];
  163. foreach ($row->getSourceProperty('field_compositeur') as $key => $value) {
  164. $term_name = $value['value'];
  165. // check if term already exists in configured vocabulary
  166. $term = array_shift(taxonomy_term_load_multiple_by_name($term_name, 'collectionneurs'));
  167. if(!$term){
  168. // if not create the term and get the tid
  169. $term = \Drupal\taxonomy\Entity\Term::create([
  170. 'vid' => 'collectionneurs',
  171. 'name' => $term_name,
  172. ]);
  173. $term->save();
  174. }
  175. // record the tid in array
  176. $collectionneurs[] = $term->get('tid')->value;
  177. }
  178. $row->setSourceProperty('collectionneurs', $collectionneurs);
  179. // son
  180. // drush_print('field_mp3 : ', 0, null, false);
  181. // drush_print_r($row->getSourceProperty('field_mp3'));
  182. $sons = [];
  183. foreach ($row->getSourceProperty('field_mp3') as $key => $value) {
  184. $sons[] = array($value['fid']);
  185. }
  186. $row->setSourceProperty('sons_fid', $sons);
  187. // get the workflow state (will be converted on status)
  188. $query = $this->select('workflow_node', 'wn');
  189. $query->fields('wn', ['sid']);
  190. $query->condition('wn.nid', $row_nid);
  191. $results = $query->execute()->fetchField();
  192. // drush_print('workflow query results : ', 0, null, false);
  193. // drush_print_r($results);
  194. // $row->setSourceProperty('workflow', $results);
  195. $row = parent::prepareRow($row);
  196. return $row;
  197. }
  198. }