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.
This commit is contained in:
parent
0839ceb2d8
commit
0f38648507
1 changed files with 7 additions and 12 deletions
|
@ -43,15 +43,10 @@ function migration_helpers_move_terms(
|
||||||
$destination_vocabulary,
|
$destination_vocabulary,
|
||||||
$source_field,
|
$source_field,
|
||||||
$destination_field,
|
$destination_field,
|
||||||
$source_bundle,
|
$bundle = NULL,
|
||||||
$destination_bundle = NULL,
|
$entity_type = 'node',
|
||||||
$source_entity_type = 'node',
|
|
||||||
$destination_entity_type = 'node',
|
|
||||||
$mapping = [],
|
$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 */
|
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
|
||||||
$entity_type_manager = \Drupal::service('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();
|
$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) {
|
foreach ($nodes as $node) {
|
||||||
$tids = [];
|
$tids = [];
|
||||||
/** @var \Drupal\node\NodeInterface $node */
|
/** @var \Drupal\node\NodeInterface $node */
|
||||||
$orig_values = $node->get($source_field);
|
$orig_values = $node->get($source_field);
|
||||||
foreach ($orig_values as $orig_value) {
|
foreach ($orig_values as $orig_value) {
|
||||||
$tid = $orig_value->target_id;
|
$orig_tid = $orig_value->target_id;
|
||||||
$orig_term = $entity_type_manager->getStorage('taxonomy_term')->load($tid);
|
$orig_term = $entity_type_manager->getStorage('taxonomy_term')->load($orig_tid);
|
||||||
$orig = $orig_term->name->value;
|
$orig = $orig_term->name->value;
|
||||||
$term_name = $mapping[$orig] ?? $orig;
|
$term_name = $mapping[$orig] ?? $orig;
|
||||||
// If our map intentionally blanked out the term name, do not create
|
// If our map intentionally blanked out the term name, do not create
|
||||||
|
@ -94,9 +89,9 @@ function migration_helpers_move_terms(
|
||||||
]);
|
]);
|
||||||
$new_term->save();
|
$new_term->save();
|
||||||
$tid = $new_term->id();
|
$tid = $new_term->id();
|
||||||
\Drupal::logger('mass')->log(LogLevel::INFO, "Term $term_name created with TID $tid in focus area vocabulary.");
|
\Drupal::logger('mass')->log(LogLevel::INFO, "Term $term_name created with TID $tid in vocabulary $destination_vocabulary.");
|
||||||
$tids[] = $tid;
|
|
||||||
}
|
}
|
||||||
|
$tids[] = $tid;
|
||||||
}
|
}
|
||||||
// Now we have collected all the term IDs that have been created.
|
// 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
|
// We have to go through a surprising number of hoops to make our
|
||||||
|
|
Reference in a new issue