D7TaxonomyTermTags.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_tags",
  10. * source_module = "taxonomy"
  11. * )
  12. */
  13. class D7TaxonomyTermTags 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. ];
  44. return $fields;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function prepareRow(Row $row) {
  50. print("\n\n".'-- prepareRow -- '.$row->getSourceProperty('tid')." -- ".$row->getSourceProperty('name')."\n");
  51. // Get Field API field values.
  52. foreach (array_keys($this->getFields('taxonomy_term', $row->getSourceProperty('machine_name'))) as $field) {
  53. $tid = $row->getSourceProperty('tid');
  54. $row->setSourceProperty($field, $this->getFieldValues('taxonomy_term', $field, $tid));
  55. }
  56. // Find parents for this row.
  57. $parents = $this->select('taxonomy_term_hierarchy', 'th')
  58. ->fields('th', ['parent', 'tid'])
  59. ->condition('tid', $row->getSourceProperty('tid'))
  60. ->execute()
  61. ->fetchCol();
  62. $row->setSourceProperty('parent', $parents);
  63. // print_r($parents);
  64. // If the term name or term description were replaced by real fields using
  65. // the Drupal 7 Title module, use the fields value instead of the term name
  66. // or term description.
  67. // TODO: translations
  68. if ($this->moduleExists('title')) {
  69. $name_field = $row->getSourceProperty('name_field');
  70. // print_r($name_field);
  71. if (isset($name_field[0]['value'])) {
  72. $row->setSourceProperty('name', $name_field[0]['value']);
  73. }
  74. $description_field = $row->getSourceProperty('description_field');
  75. if (isset($description_field[0]['value'])) {
  76. $row->setSourceProperty('description', $description_field[0]['value']);
  77. }
  78. }
  79. // Synonyms
  80. if ($this->moduleExists('title')) {
  81. $synonyms = $row->getSourceProperty('synonyms_synonym');
  82. // print_r($synonyms);
  83. if(!empty($synonyms)){
  84. $row->setSourceProperty('synonyms', $synonyms);
  85. }
  86. }
  87. return parent::prepareRow($row);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getIds() {
  93. $ids['tid']['type'] = 'integer';
  94. return $ids;
  95. }
  96. }