Skip to content

Commit

Permalink
Close #2907 Make Manual Migration Lookup DB configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
trackleft committed Nov 7, 2023
1 parent a014676 commit a36de55
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* delta: delta
* target_id:
* - plugin: az_manual_migration_lookup
* source_db_key: migrate
* source_entity_type: taxonomy_term
* source: tid
* - plugin: entity_lookup
Expand All @@ -71,6 +72,7 @@
* process:
* uid:
* - plugin: az_manual_migration_lookup
* source_db_key: custom_db
* source_entity_type: user
* source: node_uid
* - plugin: entity_lookup
Expand All @@ -87,10 +89,17 @@ class ManualMigrationLookup extends ProcessPluginBase {
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
$configuration += ['source_db_key' => 'migrate'];

if (!array_key_exists('source_entity_type', $configuration)) {
throw new \InvalidArgumentException('Manual Migration Lookup plugin is missing source_entity_type configuration. Valid values are: node, taxonomy_term.');
}

if (!is_string($configuration['source_db_key'])) {
throw new \InvalidArgumentException('Source_db_key must be a string.');
}
if (!Database::getConnectionInfo($configuration['source_db_key'])) {
throw new \InvalidArgumentException('Source_db_key must be a valid database key.');
}
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

Expand All @@ -100,27 +109,28 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {

$source_entity_type = $this->configuration['source_entity_type'];
$source_db_key = $this->configuration['source_db_key'];

$id = $value;
switch ($source_entity_type) {
case 'node':
// Lookup of content type.
$value = Database::getConnection('default', 'migrate')
$value = Database::getConnection('default', $source_db_key)
->query('SELECT title FROM {node} WHERE nid = :nid', [':nid' => $id])
->fetchField();
break;

case 'taxonomy_term':
// Lookup of taxonomy term.
$value = Database::getConnection('default', 'migrate')
$value = Database::getConnection('default', $source_db_key)
->query('SELECT name FROM {taxonomy_term_data} WHERE tid = :tid', [':tid' => $id])
->fetchField();

break;

case 'user':
// Lookup of user.
$value = Database::getConnection('default', 'migrate')
$value = Database::getConnection('default', $source_db_key)
->query('SELECT name FROM {users} WHERE uid = :uid', [':uid' => $id])
->fetchField();
break;
Expand Down

0 comments on commit a36de55

Please sign in to comment.