3 namespace Drupal\taxonomy\Plugin\migrate\source\d6;
5 use Drupal\migrate\Row;
8 * Gets i18n taxonomy terms from source database.
11 * id = "d6_term_localized_translation",
12 * source_module = "i18ntaxonomy"
15 class TermLocalizedTranslation extends Term {
20 public function query() {
21 // Ideally, the query would return rows for each language for each taxonomy
22 // term with the translations for both the name and description or just the
23 // name translation or just the description translation. That query quickly
24 // became complex and would be difficult to maintain.
25 // Therefore, build a query based on i18nstrings table where each row has
26 // the translation for only one property, either name or description. The
27 // method prepareRow() is then used to obtain the translation for the other
29 $query = parent::query();
30 $query->addField('td', 'language', 'td.language');
32 // Add in the property, which is either name or description.
33 // Cast td.tid as char for PostgreSQL compatibility.
34 $query->leftJoin('i18n_strings', 'i18n', 'CAST(td.tid AS CHAR(255)) = i18n.objectid');
35 $query->isNotNull('i18n.lid');
36 $query->addField('i18n', 'lid');
37 $query->addField('i18n', 'property');
39 // Add in the translation for the property.
40 $query->innerJoin('locales_target', 'lt', 'i18n.lid = lt.lid');
41 $query->addField('lt', 'language', 'lt.language');
42 $query->addField('lt', 'translation');
49 public function prepareRow(Row $row) {
50 $language = $row->getSourceProperty('ltlanguage');
51 $row->setSourceProperty('language', $language);
52 $tid = $row->getSourceProperty('tid');
54 // If this row has been migrated it is a duplicate then skip it.
55 if ($this->idMap->lookupDestinationIds(['tid' => $tid, 'language' => $language])) {
59 // Save the translation for this property.
60 $property = $row->getSourceProperty('property');
61 $row->setSourceProperty($property . '_translated', $row->getSourceProperty('translation'));
63 // Get the translation, if one exists, for the property not already in the
65 $other_property = ($property == 'name') ? 'description' : 'name';
66 $query = $this->select('i18n_strings', 'i18n')
67 ->fields('i18n', ['lid'])
68 ->condition('i18n.property', $other_property)
69 ->condition('i18n.objectid', $tid);
70 $query->leftJoin('locales_target', 'lt', 'i18n.lid = lt.lid');
71 $query->condition('lt.language', $language);
72 $query->addField('lt', 'translation');
73 $results = $query->execute()->fetchAssoc();
74 $row->setSourceProperty($other_property . '_translated', $results['translation']);
76 parent::prepareRow($row);
82 public function fields() {
84 'language' => $this->t('Language for this term.'),
85 'name_translated' => $this->t('Term name translation.'),
86 'description_translated' => $this->t('Term description translation.'),
88 return parent::fields() + $fields;
94 public function getIds() {
95 $ids['language']['type'] = 'string';
96 $ids['language']['alias'] = 'lt';
97 return parent::getIds() + $ids;