3 namespace Drupal\taxonomy\Plugin\migrate\source\d7;
5 use Drupal\migrate\Row;
6 use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
9 * Taxonomy term source from database.
11 * @todo Support term_relation, term_synonym table if possible.
14 * id = "d7_taxonomy_term",
15 * source_module = "taxonomy"
18 class Term extends FieldableEntity {
23 public function query() {
24 $query = $this->select('taxonomy_term_data', 'td')
28 $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
29 $query->addField('tv', 'machine_name');
31 if (isset($this->configuration['bundle'])) {
32 $query->condition('tv.machine_name', (array) $this->configuration['bundle'], 'IN');
41 public function fields() {
43 'tid' => $this->t('The term ID.'),
44 'vid' => $this->t('Existing term VID'),
45 'machine_name' => $this->t('Vocabulary machine name'),
46 'name' => $this->t('The name of the term.'),
47 'description' => $this->t('The term description.'),
48 'weight' => $this->t('Weight'),
49 'parent' => $this->t("The Drupal term IDs of the term's parents."),
50 'format' => $this->t("Format of the term description."),
58 public function prepareRow(Row $row) {
59 $tid = $row->getSourceProperty('tid');
60 $vocabulary = $row->getSourceProperty('machine_name');
61 $default_language = (array) $this->variableGet('language_default', ['language' => 'en']);
63 // If this entity was translated using Entity Translation, we need to get
64 // its source language to get the field values in the right language.
65 // The translations will be migrated by the d7_node_entity_translation
67 $translatable_vocabularies = array_keys(array_filter($this->variableGet('entity_translation_taxonomy', [])));
68 $entity_translatable = $this->isEntityTranslatable('taxonomy_term') && in_array($vocabulary, $translatable_vocabularies, TRUE);
69 $source_language = $this->getEntityTranslationSourceLanguage('taxonomy_term', $tid);
70 $language = $entity_translatable && $source_language ? $source_language : $default_language['language'];
71 $row->setSourceProperty('language', $language);
73 // Get Field API field values.
74 foreach ($this->getFields('taxonomy_term', $vocabulary) as $field_name => $field) {
75 // Ensure we're using the right language if the entity and the field are
77 $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
78 $row->setSourceProperty($field_name, $this->getFieldValues('taxonomy_term', $field_name, $tid, NULL, $field_language));
81 // Find parents for this row.
82 $parents = $this->select('taxonomy_term_hierarchy', 'th')
83 ->fields('th', ['parent', 'tid'])
84 ->condition('tid', $row->getSourceProperty('tid'))
87 $row->setSourceProperty('parent', $parents);
89 // Determine if this is a forum container.
90 $forum_container_tids = $this->variableGet('forum_containers', []);
91 $current_tid = $row->getSourceProperty('tid');
92 $row->setSourceProperty('is_container', in_array($current_tid, $forum_container_tids));
94 // If the term name or term description were replaced by real fields using
95 // the Drupal 7 Title module, use the fields value instead of the term name
96 // or term description.
97 if ($this->moduleExists('title')) {
98 $name_field = $row->getSourceProperty('name_field');
99 if (isset($name_field[0]['value'])) {
100 $row->setSourceProperty('name', $name_field[0]['value']);
102 $description_field = $row->getSourceProperty('description_field');
103 if (isset($description_field[0]['value'])) {
104 $row->setSourceProperty('description', $description_field[0]['value']);
106 if (isset($description_field[0]['format'])) {
107 $row->setSourceProperty('format', $description_field[0]['format']);
111 return parent::prepareRow($row);
117 public function getIds() {
118 $ids['tid']['type'] = 'integer';