983c3159914a6126ec821be8760507e417e32713
[yaffs-website] / web / core / modules / menu_link_content / src / Plugin / migrate / source / d6 / MenuLinkTranslation.php
1 <?php
2
3 namespace Drupal\menu_link_content\Plugin\migrate\source\d6;
4
5 use Drupal\content_translation\Plugin\migrate\source\I18nQueryTrait;
6 use Drupal\migrate\Row;
7 use Drupal\menu_link_content\Plugin\migrate\source\MenuLink;
8
9 /**
10  * Gets Menu link translations from source database.
11  *
12  * @MigrateSource(
13  *   id = "d6_menu_link_translation",
14  *   source_module = "i18nmenu"
15  * )
16  */
17 class MenuLinkTranslation extends MenuLink {
18
19   use I18nQueryTrait;
20
21   /**
22    * Drupal 6 table names.
23    */
24   const I18N_STRING_TABLE = 'i18n_strings';
25
26   /**
27    * {@inheritdoc}
28    */
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
37     // property.
38     // The query starts with the same query as menu_link.
39     $query = parent::query();
40
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');
47
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');
52     return $query;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function prepareRow(Row $row) {
59     parent::prepareRow($row);
60
61     // Save the translation for this property.
62     $property_in_row = $row->getSourceProperty('property');
63
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
67     // in the row.
68     $property_not_in_row = ($property_in_row == 'title') ? 'description' : 'title';
69     return $this->getPropertyNotInRowTranslation($row, $property_not_in_row, 'mlid', $this->idMap);
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function fields() {
76     $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.'),
80     ];
81     return parent::fields() + $fields;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getIds() {
88     $ids['language']['type'] = 'string';
89     return parent::getIds() + $ids;
90   }
91
92 }