3 namespace Drupal\menu_link_content\Plugin\migrate\source\d6;
5 use Drupal\content_translation\Plugin\migrate\source\I18nQueryTrait;
6 use Drupal\migrate\Row;
7 use Drupal\menu_link_content\Plugin\migrate\source\MenuLink;
10 * Gets Menu link translations from source database.
13 * id = "d6_menu_link_translation",
14 * source_module = "i18nmenu"
17 class MenuLinkTranslation extends MenuLink {
22 * Drupal 6 table names.
24 const I18N_STRING_TABLE = 'i18n_strings';
29 public function query() {
30 // Ideally, the query would return rows for each language for each menu link
31 // with the translations for both the title and description or just the
32 // title translation or just the description translation. That query quickly
33 // became complex and would be difficult to maintain.
34 // Therefore, build a query based on i18nstrings table where each row has
35 // the translation for only one property, either title or description. The
36 // method prepareRow() is then used to obtain the translation for the other
38 // The query starts with the same query as menu_link.
39 $query = parent::query();
41 // Add in the property, which is either title or description. Cast the mlid
42 // to text so PostgreSQL can make the join.
43 $query->leftJoin(static::I18N_STRING_TABLE, 'i18n', 'CAST(ml.mlid as CHAR(255)) = i18n.objectid');
44 $query->isNotNull('i18n.lid');
45 $query->addField('i18n', 'lid');
46 $query->addField('i18n', 'property');
48 // Add in the translation for the property.
49 $query->innerJoin('locales_target', 'lt', 'i18n.lid = lt.lid');
50 $query->addField('lt', 'language');
51 $query->addField('lt', 'translation');
58 public function prepareRow(Row $row) {
59 parent::prepareRow($row);
61 // Save the translation for this property.
62 $property_in_row = $row->getSourceProperty('property');
64 // Set the i18n string table for use in I18nQueryTrait.
65 $this->i18nStringTable = static::I18N_STRING_TABLE;
66 // Get the translation for the property not already in the row and save it
68 $property_not_in_row = ($property_in_row == 'title') ? 'description' : 'title';
69 return $this->getPropertyNotInRowTranslation($row, $property_not_in_row, 'mlid', $this->idMap);
75 public function fields() {
77 'language' => $this->t('Language for this menu.'),
78 'title_translated' => $this->t('Menu link title translation.'),
79 'description_translated' => $this->t('Menu link description translation.'),
81 return parent::fields() + $fields;
87 public function getIds() {
88 $ids['language']['type'] = 'string';
89 return parent::getIds() + $ids;