D7TaxonomyTermThesaurus.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ->condition('et.entity_type', 'taxonomy_term')
  21. ->condition('et.source', '')
  22. ->orderBy('tid');
  23. $query->innerJoin('entity_translation', 'et', 'et.entity_id = td.tid');
  24. $query->fields('et');
  25. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  26. $query->addField('tv', 'machine_name');
  27. if (isset($this->configuration['bundle'])) {
  28. $query->condition('tv.machine_name', (array) $this->configuration['bundle'], 'IN');
  29. }
  30. return $query;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function fields() {
  36. $fields = [
  37. 'tid' => $this->t('The term ID.'),
  38. 'vid' => $this->t('Existing term VID'),
  39. 'machine_name' => $this->t('Vocabulary machine name'),
  40. 'name' => $this->t('The name of the term.'),
  41. 'description' => $this->t('The term description.'),
  42. 'weight' => $this->t('Weight'),
  43. 'parent' => $this->t("The Drupal term IDs of the term's parents."),
  44. 'format' => $this->t("Format of the term description."),
  45. 'synonyms' => $this->t("Term's Synonyms (field : synonyms_synonym)"),
  46. 'displayed_on_advanced_search' => $this->t("field_used_on_advanced_search"),
  47. 'revision_id' => $this->t('Revision ID'),
  48. 'language' => $this->t('Term translation language'),
  49. 'source' => $this->t('Term translation source language'),
  50. ];
  51. return $fields;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function prepareRow(Row $row) {
  57. $language = $row->getSourceProperty('language');
  58. drush_print('-- '.$language.' -- '.$row->getSourceProperty('tid')." -- ".$row->getSourceProperty('name')." -- ".$row->getSourceProperty('weight'));
  59. $tid = $row->getSourceProperty('tid');
  60. // vocabulary machine name
  61. $machine_name = $row->getSourceProperty('machine_name');
  62. $revid = $row->getSourceProperty('revision_id');
  63. // Get Field API field values.
  64. foreach ($this->getFields('taxonomy_term', $machine_name) as $field_name => $field) {
  65. // Ensure we're using the right language if the entity is translatable.
  66. $field_language = $field['translatable'] ? $language : NULL;
  67. $row->setSourceProperty($field_name, $this->getFieldValues('taxonomy_term', $field_name, $tid, $revid, $field_language));
  68. }
  69. // Find parents for this row.
  70. $parents = $this->select('taxonomy_term_hierarchy', 'th')
  71. ->fields('th', ['parent', 'tid'])
  72. ->condition('tid', $row->getSourceProperty('tid'))
  73. ->execute()
  74. ->fetchCol();
  75. $row->setSourceProperty('parent', $parents);
  76. // If the node title was replaced by a real field using the Drupal 7 Title
  77. // module, use the field value instead of the node title.
  78. if ($this->moduleExists('title')) {
  79. $name_field = $row->getSourceProperty('name_field');
  80. if (isset($name_field[0]['value'])) {
  81. $row->setSourceProperty('name', $name_field[0]['value']);
  82. }
  83. }
  84. return parent::prepareRow($row);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getIds() {
  90. $ids['tid']['type'] = 'integer';
  91. return $ids;
  92. }
  93. }