Issue #2918295 by quietone, heddn, Jo Fitzgerald, phenaproxima, maxocub: Move i18n query to a trait
parent
2dabd1de10
commit
e22910b9d9
|
@ -4,6 +4,7 @@ namespace Drupal\block_content\Plugin\migrate\source\d6;
|
|||
|
||||
use Drupal\migrate\Row;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
|
||||
use Drupal\content_translation\Plugin\migrate\source\d6\I18nQueryTrait;
|
||||
|
||||
/**
|
||||
* Gets Drupal 6 i18n custom block translations from database.
|
||||
|
@ -15,6 +16,8 @@ use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
|
|||
*/
|
||||
class BoxTranslation extends DrupalSqlBase {
|
||||
|
||||
use I18nQueryTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -48,34 +51,13 @@ class BoxTranslation extends DrupalSqlBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function prepareRow(Row $row) {
|
||||
$language = $row->getSourceProperty('language');
|
||||
$bid = $row->getSourceProperty('bid');
|
||||
|
||||
// If this row has been migrated it is a duplicate then skip it.
|
||||
if ($this->idMap->lookupDestinationIds(['bid' => $bid, 'language' => $language])) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
parent::prepareRow($row);
|
||||
// Save the translation for this property.
|
||||
$property = $row->getSourceProperty('property');
|
||||
$row->setSourceProperty($property . '_translated', $row->getSourceProperty('translation'));
|
||||
|
||||
// Get the translation for the property not already in the row.
|
||||
$translation = ($property === 'title') ? 'body' : 'title';
|
||||
$query = $this->select('i18n_strings', 'i18n')
|
||||
->fields('i18n', ['lid'])
|
||||
->condition('i18n.property', $translation)
|
||||
->condition('i18n.objectid', $bid);
|
||||
$query->leftJoin('locales_target', 'lt', 'i18n.lid = lt.lid');
|
||||
$query->condition('lt.language', $language)
|
||||
->addField('lt', 'translation');
|
||||
$results = $query->execute()->fetchAssoc();
|
||||
if (!$results) {
|
||||
$row->setSourceProperty($translation . '_translated', NULL);
|
||||
}
|
||||
else {
|
||||
$row->setSourceProperty($translation . '_translated', $results['translation']);
|
||||
}
|
||||
$property_in_row = $row->getSourceProperty('property');
|
||||
// Get the translation for the property not already in the row and save it
|
||||
// in the row.
|
||||
$property_not_in_row = ($property_in_row === 'title') ? 'body' : 'title';
|
||||
return $this->getPropertyNotInRowTranslation($row, $property_not_in_row, 'bid', $this->idMap);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\content_translation\Plugin\migrate\source\d6;
|
||||
|
||||
use Drupal\migrate\Plugin\MigrateIdMapInterface;
|
||||
use Drupal\migrate\MigrateException;
|
||||
use Drupal\migrate\Row;
|
||||
|
||||
/**
|
||||
* Gets an i18n translation from the source database.
|
||||
*/
|
||||
trait I18nQueryTrait {
|
||||
|
||||
/**
|
||||
* Get the translation for the property not already in the row.
|
||||
*
|
||||
* For some i18n migrations there are two translation values, such as a
|
||||
* translated title and a translated description, that need to be retrieved.
|
||||
* Since these values are stored in separate rows of the i18n_strings
|
||||
* table we get them individually, one in the source plugin query() and the
|
||||
* other in prepareRow(). The names of the properties varies, for example,
|
||||
* in BoxTranslation they are 'body' and 'title' whereas in
|
||||
* MenuLinkTranslation they are 'title' and 'description'. This will save both
|
||||
* translations to the row.
|
||||
*
|
||||
* @param \Drupal\migrate\Row $row
|
||||
* The current migration row which must include both a 'language' property
|
||||
* and an 'objectid' property. The 'objectid' is the value for the
|
||||
* 'objectid' field in the i18n_strings table.
|
||||
* @param string $property_not_in_row
|
||||
* The name of the property to get the translation for.
|
||||
* @param string $object_id_name
|
||||
* The value of the objectid in the i18n table.
|
||||
* @param \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map
|
||||
* The ID map.
|
||||
*
|
||||
* @return bool
|
||||
* FALSE if the property has already been migrated.
|
||||
*
|
||||
* @throws \Drupal\migrate\MigrateException
|
||||
*/
|
||||
protected function getPropertyNotInRowTranslation(Row $row, $property_not_in_row, $object_id_name, MigrateIdMapInterface $id_map) {
|
||||
$language = $row->getSourceProperty('language');
|
||||
if (!$language) {
|
||||
throw new MigrateException('No language found.');
|
||||
}
|
||||
$object_id = $row->getSourceProperty($object_id_name);
|
||||
if (!$object_id) {
|
||||
throw new MigrateException('No objectid found.');
|
||||
}
|
||||
|
||||
// If this row has been migrated it is a duplicate so skip it.
|
||||
if ($id_map->lookupDestinationIds([$object_id_name => $object_id, 'language' => $language])) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Save the translation for the property already in the row.
|
||||
$property_in_row = $row->getSourceProperty('property');
|
||||
$row->setSourceProperty($property_in_row . '_translated', $row->getSourceProperty('translation'));
|
||||
|
||||
// Get the translation, if one exists, for the property not already in the
|
||||
// row.
|
||||
$query = $this->select('i18n_strings', 'i18n')
|
||||
->fields('i18n', ['lid'])
|
||||
->condition('i18n.property', $property_not_in_row)
|
||||
->condition('i18n.objectid', $object_id);
|
||||
$query->leftJoin('locales_target', 'lt', 'i18n.lid = lt.lid');
|
||||
$query->condition('lt.language', $language);
|
||||
$query->addField('lt', 'translation');
|
||||
$results = $query->execute()->fetchAssoc();
|
||||
if (!$results) {
|
||||
$row->setSourceProperty($property_not_in_row . '_translated', NULL);
|
||||
}
|
||||
else {
|
||||
$row->setSourceProperty($property_not_in_row . '_translated', $results['translation']);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Drupal\menu_link_content\Plugin\migrate\source\d6;
|
||||
|
||||
use Drupal\content_translation\Plugin\migrate\source\d6\I18nQueryTrait;
|
||||
use Drupal\migrate\Row;
|
||||
use Drupal\menu_link_content\Plugin\migrate\source\MenuLink;
|
||||
|
||||
|
@ -15,6 +16,8 @@ use Drupal\menu_link_content\Plugin\migrate\source\MenuLink;
|
|||
*/
|
||||
class MenuLinkTranslation extends MenuLink {
|
||||
|
||||
use I18nQueryTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -48,31 +51,15 @@ class MenuLinkTranslation extends MenuLink {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function prepareRow(Row $row) {
|
||||
$language = $row->getSourceProperty('language');
|
||||
$mlid = $row->getSourceProperty('mlid');
|
||||
|
||||
// If this row has been migrated it is a duplicate then skip it.
|
||||
if ($this->idMap->lookupDestinationIds(['mlid' => $mlid, 'language' => $language])) {
|
||||
return FALSE;
|
||||
}
|
||||
parent::prepareRow($row);
|
||||
|
||||
// Save the translation for this property.
|
||||
$property = $row->getSourceProperty('property');
|
||||
$row->setSourceProperty($property . '_translated', $row->getSourceProperty('translation'));
|
||||
$property_in_row = $row->getSourceProperty('property');
|
||||
|
||||
// Get the translation, if one exists, for the property not already in the
|
||||
// row.
|
||||
$other_property = ($property == 'title') ? 'description' : 'title';
|
||||
$query = $this->select('i18n_strings', 'i18n')
|
||||
->fields('i18n', ['lid'])
|
||||
->condition('i18n.property', $other_property)
|
||||
->condition('i18n.objectid', $mlid);
|
||||
$query->leftJoin('locales_target', 'lt', 'i18n.lid = lt.lid');
|
||||
$query->condition('lt.language', $language);
|
||||
$query->addField('lt', 'translation');
|
||||
$results = $query->execute()->fetchAssoc();
|
||||
$row->setSourceProperty($other_property . '_translated', $results['translation']);
|
||||
parent::prepareRow($row);
|
||||
// Get the translation for the property not already in the row and save it
|
||||
// in the row.
|
||||
$property_not_in_row = ($property_in_row == 'title') ? 'description' : 'title';
|
||||
return $this->getPropertyNotInRowTranslation($row, $property_not_in_row, 'mlid', $this->idMap);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue