View source
<?php
namespace Drupal\taxonomy\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
class Term extends FieldableEntity {
public function query() {
$query = $this
->select('taxonomy_term_data', 'td')
->fields('td')
->distinct()
->orderBy('tid');
$query
->leftJoin('taxonomy_vocabulary', 'tv', '[td].[vid] = [tv].[vid]');
$query
->addField('tv', 'machine_name');
if ($this
->getDatabase()
->schema()
->fieldExists('taxonomy_vocabulary', 'i18n_mode')) {
$query
->addField('tv', 'i18n_mode');
}
if (isset($this->configuration['bundle'])) {
$query
->condition('tv.machine_name', (array) $this->configuration['bundle'], 'IN');
}
return $query;
}
public function fields() {
$fields = [
'tid' => $this
->t('The term ID.'),
'vid' => $this
->t('Existing term VID'),
'machine_name' => $this
->t('Vocabulary machine name'),
'name' => $this
->t('The name of the term.'),
'description' => $this
->t('The term description.'),
'weight' => $this
->t('Weight'),
'parent' => $this
->t("The Drupal term IDs of the term's parents."),
'format' => $this
->t("Format of the term description."),
];
return $fields;
}
public function prepareRow(Row $row) {
$tid = $row
->getSourceProperty('tid');
$vocabulary = $row
->getSourceProperty('machine_name');
$default_language = (array) $this
->variableGet('language_default', [
'language' => 'en',
]);
$translatable_vocabularies = array_keys(array_filter($this
->variableGet('entity_translation_taxonomy', [])));
$entity_translatable = $this
->isEntityTranslatable('taxonomy_term') && in_array($vocabulary, $translatable_vocabularies, TRUE);
if ($entity_translatable) {
$source_language = $this
->getEntityTranslationSourceLanguage('taxonomy_term', $tid);
$language = $entity_translatable && $source_language ? $source_language : $default_language['language'];
}
if ($row
->get('i18n_mode')) {
$language = $row
->get('i18n_mode') === '1' ? $default_language['language'] : $row
->get('language');
}
$language = $language ?? $default_language['language'];
$row
->setSourceProperty('language', $language);
foreach ($this
->getFields('taxonomy_term', $vocabulary) as $field_name => $field) {
$field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
$row
->setSourceProperty($field_name, $this
->getFieldValues('taxonomy_term', $field_name, $tid, NULL, $field_language));
}
$parents = $this
->select('taxonomy_term_hierarchy', 'th')
->fields('th', [
'parent',
'tid',
])
->condition('tid', $row
->getSourceProperty('tid'))
->execute()
->fetchCol();
$row
->setSourceProperty('parent', $parents);
$forum_container_tids = $this
->variableGet('forum_containers', []);
$current_tid = $row
->getSourceProperty('tid');
$row
->setSourceProperty('is_container', in_array($current_tid, $forum_container_tids));
if ($this
->moduleExists('title')) {
$name_field = $row
->getSourceProperty('name_field');
if (isset($name_field[0]['value'])) {
$row
->setSourceProperty('name', $name_field[0]['value']);
}
$description_field = $row
->getSourceProperty('description_field');
if (isset($description_field[0]['value'])) {
$row
->setSourceProperty('description', $description_field[0]['value']);
}
if (isset($description_field[0]['format'])) {
$row
->setSourceProperty('format', $description_field[0]['format']);
}
}
return parent::prepareRow($row);
}
public function getIds() {
$ids['tid']['type'] = 'integer';
return $ids;
}
}