From 0839ceb2d8868de25e3bfa840c3009991389bf65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?benjamin=20melan=C3=A7on?= Date: Thu, 16 Nov 2023 10:26:59 -0500 Subject: [PATCH 1/2] Not quite working take 1 --- migration_helpers.module | 68 ++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/migration_helpers.module b/migration_helpers.module index b443a0b..b04ca01 100644 --- a/migration_helpers.module +++ b/migration_helpers.module @@ -1,5 +1,7 @@ id()] = $term->label(); + } + + $nodes = migration_helpers_entity_query($source_entity_type, $source_bundle, $source_field); + foreach ($nodes as $node) { + $tids = []; + /** @var \Drupal\node\NodeInterface $node */ + $orig_values = $node->get($source_field); + foreach ($orig_values as $orig_value) { + $tid = $orig_value->target_id; + $orig_term = $entity_type_manager->getStorage('taxonomy_term')->load($tid); + $orig = $orig_term->name->value; + $term_name = $mapping[$orig] ?? $orig; + // If our map intentionally blanked out the term name, do not create + // a term in the new vocabulary and assign it to the destination field. + // Note, the term must be blanked out ('') or assigned to FALSE in the + // mapping, setting a term to NULL is the same as not mapping it, and it + // would be created in the new vocab etc. + if (!$term_name) { + continue; + } + $tid = array_search($term_name, $term_data); + if (empty($tid)) { + $new_term = $entity_type_manager->getStorage('taxonomy_term')->create([ + 'name' => $term_name, + 'vid' => $destination_vocabulary, + ]); + $new_term->save(); + $tid = $new_term->id(); + \Drupal::logger('mass')->log(LogLevel::INFO, "Term $term_name created with TID $tid in focus area vocabulary."); + $tids[] = $tid; + } + } + // Now we have collected all the term IDs that have been created. + // We have to go through a surprising number of hoops to make our + $already_there_tids = []; + $already_there_values = $node->get($destination_field); + foreach ($already_there_values as $already_there_value) { + $already_there_tids[] = $already_there_value->target_id; + } + $node->set($destination_field, array_merge($tids, $already_there_tids)); + $node->save(); + } } // The below almost serve more as examples that can be followed From 0f3864850796e2e7a180704b88e44c3d8bacd17e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?benjamin=20melan=C3=A7on?= Date: Thu, 16 Nov 2023 11:06:05 -0500 Subject: [PATCH 2/2] Simplify and fix That last bit of the tids being assigned *outside* the if loop (so existing, not only created ones, get used) is key. --- migration_helpers.module | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/migration_helpers.module b/migration_helpers.module index b04ca01..4710a58 100644 --- a/migration_helpers.module +++ b/migration_helpers.module @@ -43,15 +43,10 @@ function migration_helpers_move_terms( $destination_vocabulary, $source_field, $destination_field, - $source_bundle, - $destination_bundle = NULL, - $source_entity_type = 'node', - $destination_entity_type = 'node', + $bundle = NULL, + $entity_type = 'node', $mapping = [], ) { - // Destination bundle is also required but we default to making it - // the same as the source bundle. - $destination_bundle ??= $source_bundle; /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ $entity_type_manager = \Drupal::service('entity_type.manager'); @@ -68,14 +63,14 @@ function migration_helpers_move_terms( $term_data[$term->id()] = $term->label(); } - $nodes = migration_helpers_entity_query($source_entity_type, $source_bundle, $source_field); + $nodes = migration_helpers_entity_query($entity_type, $bundle, $source_field); foreach ($nodes as $node) { $tids = []; /** @var \Drupal\node\NodeInterface $node */ $orig_values = $node->get($source_field); foreach ($orig_values as $orig_value) { - $tid = $orig_value->target_id; - $orig_term = $entity_type_manager->getStorage('taxonomy_term')->load($tid); + $orig_tid = $orig_value->target_id; + $orig_term = $entity_type_manager->getStorage('taxonomy_term')->load($orig_tid); $orig = $orig_term->name->value; $term_name = $mapping[$orig] ?? $orig; // If our map intentionally blanked out the term name, do not create @@ -94,9 +89,9 @@ function migration_helpers_move_terms( ]); $new_term->save(); $tid = $new_term->id(); - \Drupal::logger('mass')->log(LogLevel::INFO, "Term $term_name created with TID $tid in focus area vocabulary."); - $tids[] = $tid; + \Drupal::logger('mass')->log(LogLevel::INFO, "Term $term_name created with TID $tid in vocabulary $destination_vocabulary."); } + $tids[] = $tid; } // Now we have collected all the term IDs that have been created. // We have to go through a surprising number of hoops to make our