From e22910b9d995a03593eb3e3064d803cbc267f755 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 20 Sep 2018 17:19:17 +0100 Subject: [PATCH] Issue #2918295 by quietone, heddn, Jo Fitzgerald, phenaproxima, maxocub: Move i18n query to a trait --- .../migrate/source/d6/BoxTranslation.php | 36 +++------ .../migrate/source/d6/I18nQueryTrait.php | 80 +++++++++++++++++++ .../migrate/source/d6/MenuLinkTranslation.php | 31 +++---- 3 files changed, 98 insertions(+), 49 deletions(-) create mode 100644 core/modules/content_translation/src/Plugin/migrate/source/d6/I18nQueryTrait.php diff --git a/core/modules/block_content/src/Plugin/migrate/source/d6/BoxTranslation.php b/core/modules/block_content/src/Plugin/migrate/source/d6/BoxTranslation.php index d5dffd961ad..18a5c86b51a 100644 --- a/core/modules/block_content/src/Plugin/migrate/source/d6/BoxTranslation.php +++ b/core/modules/block_content/src/Plugin/migrate/source/d6/BoxTranslation.php @@ -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); } /** diff --git a/core/modules/content_translation/src/Plugin/migrate/source/d6/I18nQueryTrait.php b/core/modules/content_translation/src/Plugin/migrate/source/d6/I18nQueryTrait.php new file mode 100644 index 00000000000..a5bc9494f2a --- /dev/null +++ b/core/modules/content_translation/src/Plugin/migrate/source/d6/I18nQueryTrait.php @@ -0,0 +1,80 @@ +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; + } + +} diff --git a/core/modules/menu_link_content/src/Plugin/migrate/source/d6/MenuLinkTranslation.php b/core/modules/menu_link_content/src/Plugin/migrate/source/d6/MenuLinkTranslation.php index 50b4d144221..6f458e5ad1f 100644 --- a/core/modules/menu_link_content/src/Plugin/migrate/source/d6/MenuLinkTranslation.php +++ b/core/modules/menu_link_content/src/Plugin/migrate/source/d6/MenuLinkTranslation.php @@ -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); } /**