3 namespace Drupal\content_translation\Plugin\migrate\source;
5 use Drupal\migrate\Plugin\MigrateIdMapInterface;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\Row;
10 * Gets an i18n translation from the source database.
12 trait I18nQueryTrait {
15 * The i18n string table name.
19 protected $i18nStringTable;
22 * Gets the translation for the property not already in the row.
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.
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
45 * FALSE if the property has already been migrated.
47 * @throws \Drupal\migrate\MigrateException
49 protected function getPropertyNotInRowTranslation(Row $row, $property_not_in_row, $object_id_name, MigrateIdMapInterface $id_map) {
50 $language = $row->getSourceProperty('language');
52 throw new MigrateException('No language found.');
54 $object_id = $row->getSourceProperty($object_id_name);
56 throw new MigrateException('No objectid found.');
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])) {
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'));
68 // Get the translation, if one exists, for the property not already in the
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();
79 $row->setSourceProperty($property_not_in_row . '_translated', NULL);
82 $row->setSourceProperty($property_not_in_row . '_translated', $results['translation']);