D7TaxonomyTermThesaurus.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  5. /**
  6. * Taxonomy term source from database.
  7. *
  8. * @MigrateSource(
  9. * id = "d7_taxonomy_term_thesaurus",
  10. * source_module = "taxonomy"
  11. * )
  12. */
  13. class D7TaxonomyTermThesaurus extends FieldableEntity {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function query() {
  18. $query = $this->select('taxonomy_term_data', 'td')
  19. ->fields('td')
  20. ->distinct()
  21. ->orderBy('tid');
  22. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  23. $query->addField('tv', 'machine_name');
  24. if (isset($this->configuration['bundle'])) {
  25. $query->condition('tv.machine_name', (array) $this->configuration['bundle'], 'IN');
  26. }
  27. return $query;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function fields() {
  33. $fields = [
  34. 'tid' => $this->t('The term ID.'),
  35. 'vid' => $this->t('Existing term VID'),
  36. 'machine_name' => $this->t('Vocabulary machine name'),
  37. 'name' => $this->t('The name of the term.'),
  38. 'description' => $this->t('The term description.'),
  39. 'weight' => $this->t('Weight'),
  40. 'parent' => $this->t("The Drupal term IDs of the term's parents."),
  41. 'format' => $this->t("Format of the term description."),
  42. 'synonyms' => $this->t("Term's Synonyms (field : synonyms_synonym)"),
  43. 'displayed_on_advanced_search' => $this->t("field_used_on_advanced_search"),
  44. ];
  45. return $fields;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function prepareRow(Row $row) {
  51. print("\n\n".'-- prepareRow -- '.$row->getSourceProperty('tid')." -- ".$row->getSourceProperty('name')." -- ".$row->getSourceProperty('weight')."\n");
  52. // Get Field API field values.
  53. // foreach (array_keys($this->getFields('taxonomy_term', $row->getSourceProperty('machine_name'))) as $field) {
  54. // $tid = $row->getSourceProperty('tid');
  55. // $row->setSourceProperty($field, $this->getFieldValues('taxonomy_term', $field, $tid));
  56. // print("field: ".$field."\n");
  57. // print_r($this->getFieldValues('taxonomy_term', $field, $tid));
  58. // print("-\n");
  59. // }
  60. // Find parents for this row.
  61. $parents = $this->select('taxonomy_term_hierarchy', 'th')
  62. ->fields('th', ['parent', 'tid'])
  63. ->condition('tid', $row->getSourceProperty('tid'))
  64. ->execute()
  65. ->fetchCol();
  66. $row->setSourceProperty('parent', $parents);
  67. // print_r($parents);
  68. // get the english term name as defaut
  69. $query = $this->select('entity_translation', 'et')
  70. // ->fields('et', [
  71. // 'entity_id',
  72. // 'revision_id',
  73. // 'language',
  74. // 'source',
  75. // 'uid',
  76. // 'status',
  77. // 'created',
  78. // 'changed',
  79. // ])
  80. // ->distinct()
  81. ->condition('et.entity_type', 'taxonomy_term')
  82. ->condition('et.language', 'en')
  83. ->condition('rnf.language', 'en')
  84. ->condition('et.entity_id', $row->getSourceProperty('tid'));
  85. $query->innerjoin('field_revision_name_field', 'rnf', 'rnf.revision_id = et.revision_id');
  86. $query->fields('rnf', ['name_field_value']);
  87. $name_field_en = $query->execute()->fetchField();
  88. print($name_field_en);
  89. if($name_field_en != NULL && $name_field_en != ""){
  90. $row->setSourceProperty('name', $name_field_en);
  91. }
  92. // If the term name or term description were replaced by real fields using
  93. // the Drupal 7 Title module, use the fields value instead of the term name
  94. // or term description.
  95. // if ($this->moduleExists('title')) {
  96. // $name_field = $row->getSourceProperty('name_field');
  97. // // print_r($name_field);
  98. // if (isset($name_field[0]['value'])) {
  99. // $row->setSourceProperty('name', $name_field[0]['value']);
  100. // }
  101. // $description_field = $row->getSourceProperty('description_field');
  102. // if (isset($description_field[0]['value'])) {
  103. // $row->setSourceProperty('description', $description_field[0]['value']);
  104. // }
  105. // }
  106. // Synonyms
  107. if ($this->moduleExists('title')) {
  108. $synonyms = $row->getSourceProperty('synonyms_synonym');
  109. // print_r($synonyms);
  110. if(!empty($synonyms)){
  111. $row->setSourceProperty('synonyms', $synonyms);
  112. }
  113. }
  114. // displayed_on_advanced_search
  115. $advanced_search = $row->getSourceProperty('field_used_on_advanced_search');
  116. // print_r($advanced_search);
  117. if(!empty($advanced_search)){
  118. $row->setSourceProperty('advanced_search', $advanced_search);
  119. }
  120. return parent::prepareRow($row);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getIds() {
  126. $ids['tid']['type'] = 'integer';
  127. return $ids;
  128. }
  129. }