Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / content_translation / src / Plugin / migrate / source / I18nQueryTrait.php
1 <?php
2
3 namespace Drupal\content_translation\Plugin\migrate\source;
4
5 use Drupal\migrate\Plugin\MigrateIdMapInterface;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\Row;
8
9 /**
10  * Gets an i18n translation from the source database.
11  */
12 trait I18nQueryTrait {
13
14   /**
15    * The i18n string table name.
16    *
17    * @var string
18    */
19   protected $i18nStringTable;
20
21   /**
22    * Gets the translation for the property not already in the row.
23    *
24    * For some i18n migrations there are two translation values, such as a
25    * translated title and a translated description, that need to be retrieved.
26    * Since these values are stored in separate rows of the i18nStringTable
27    * table we get them individually, one in the source plugin query() and the
28    * other in prepareRow(). The names of the properties varies, for example,
29    * in BoxTranslation they are 'body' and 'title' whereas in
30    * MenuLinkTranslation they are 'title' and 'description'. This will save both
31    * translations to the row.
32    *
33    * @param \Drupal\migrate\Row $row
34    *   The current migration row which must include both a 'language' property
35    *   and an 'objectid' property. The 'objectid' is the value for the
36    *   'objectid' field in the i18n_string table.
37    * @param string $property_not_in_row
38    *   The name of the property to get the translation for.
39    * @param string $object_id_name
40    *   The value of the objectid in the i18n table.
41    * @param \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map
42    *   The ID map.
43    *
44    * @return bool
45    *   FALSE if the property has already been migrated.
46    *
47    * @throws \Drupal\migrate\MigrateException
48    */
49   protected function getPropertyNotInRowTranslation(Row $row, $property_not_in_row, $object_id_name, MigrateIdMapInterface $id_map) {
50     $language = $row->getSourceProperty('language');
51     if (!$language) {
52       throw new MigrateException('No language found.');
53     }
54     $object_id = $row->getSourceProperty($object_id_name);
55     if (!$object_id) {
56       throw new MigrateException('No objectid found.');
57     }
58
59     // If this row has been migrated it is a duplicate so skip it.
60     if ($id_map->lookupDestinationIds([$object_id_name => $object_id, 'language' => $language])) {
61       return FALSE;
62     }
63
64     // Save the translation for the property already in the row.
65     $property_in_row = $row->getSourceProperty('property');
66     $row->setSourceProperty($property_in_row . '_translated', $row->getSourceProperty('translation'));
67
68     // Get the translation, if one exists, for the property not already in the
69     // row.
70     $query = $this->select($this->i18nStringTable, 'i18n')
71       ->fields('i18n', ['lid'])
72       ->condition('i18n.property', $property_not_in_row)
73       ->condition('i18n.objectid', $object_id);
74     $query->leftJoin('locales_target', 'lt', 'i18n.lid = lt.lid');
75     $query->condition('lt.language', $language);
76     $query->addField('lt', 'translation');
77     $results = $query->execute()->fetchAssoc();
78     if (!$results) {
79       $row->setSourceProperty($property_not_in_row . '_translated', NULL);
80     }
81     else {
82       $row->setSourceProperty($property_not_in_row . '_translated', $results['translation']);
83     }
84     return TRUE;
85   }
86
87 }