D7TaxonomyTermShowroom.php 3.0 KB

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