Pull merge.
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / migrate / source / d6 / TermLocalizedTranslation.php
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\migrate\source\d6;
4
5 use Drupal\migrate\Row;
6
7 /**
8  * Gets i18n taxonomy terms from source database.
9  *
10  * @MigrateSource(
11  *   id = "d6_term_localized_translation",
12  *   source_module = "i18ntaxonomy"
13  * )
14  */
15 class TermLocalizedTranslation extends Term {
16
17   /**
18    * {@inheritdoc}
19    */
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
28     // property.
29     $query = parent::query();
30     $query->addField('td', 'language', 'td.language');
31
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');
38
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');
43     return $query;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function prepareRow(Row $row) {
50     $language = $row->getSourceProperty('ltlanguage');
51     $row->setSourceProperty('language', $language);
52     $tid = $row->getSourceProperty('tid');
53
54     // If this row has been migrated it is a duplicate then skip it.
55     if ($this->idMap->lookupDestinationIds(['tid' => $tid, 'language' => $language])) {
56       return FALSE;
57     }
58
59     // Save the translation for this property.
60     $property = $row->getSourceProperty('property');
61     $row->setSourceProperty($property . '_translated', $row->getSourceProperty('translation'));
62
63     // Get the translation, if one exists, for the property not already in the
64     // row.
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']);
75
76     parent::prepareRow($row);
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function fields() {
83     $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.'),
87     ];
88     return parent::fields() + $fields;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getIds() {
95     $ids['language']['type'] = 'string';
96     $ids['language']['alias'] = 'lt';
97     return parent::getIds() + $ids;
98   }
99
100 }