Compare commits
No commits in common. "0f3864850796e2e7a180704b88e44c3d8bacd17e" and "9df071a382e144e47a94542e2b7c271d75a08fec" have entirely different histories.
0f38648507
...
9df071a382
1 changed files with 17 additions and 60 deletions
|
@ -1,7 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Psr\Log\LogLevel;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Entity query helper.
|
* Entity query helper.
|
||||||
*/
|
*/
|
||||||
|
@ -40,69 +38,28 @@ function migration_helpers_entity_query(
|
||||||
* Move (and optionally filter/transform) terms between term reference fields.
|
* Move (and optionally filter/transform) terms between term reference fields.
|
||||||
*/
|
*/
|
||||||
function migration_helpers_move_terms(
|
function migration_helpers_move_terms(
|
||||||
|
$source_vocabulary,
|
||||||
$destination_vocabulary,
|
$destination_vocabulary,
|
||||||
$source_field,
|
$source_field,
|
||||||
$destination_field,
|
$destination_field,
|
||||||
$bundle = NULL,
|
$source_bundle,
|
||||||
$entity_type = 'node',
|
$destination_bundle = NULL,
|
||||||
|
$source_entity_type = 'node',
|
||||||
|
$destination_entity_type = 'node',
|
||||||
$mapping = [],
|
$mapping = [],
|
||||||
) {
|
) {
|
||||||
|
// Destination bundle is also required but we default to making it
|
||||||
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
|
// the same as the source bundle.
|
||||||
$entity_type_manager = \Drupal::service('entity_type.manager');
|
$destination_bundle ??= $source_bundle;
|
||||||
|
print("
|
||||||
// Get any existing terms in destination vocabulary so we can efficiently
|
$source_entity_type,
|
||||||
// prevent duplication.
|
$source_bundle,
|
||||||
$terms = migration_helpers_entity_query(
|
$source_field,
|
||||||
entity_type: 'taxonomy_term',
|
$destination_entity_type,
|
||||||
bundle: $destination_vocabulary,
|
$destination_bundle,
|
||||||
);
|
$destination_field,
|
||||||
|
$source_vocabulary,
|
||||||
$term_data = [];
|
$destination_vocabulary");
|
||||||
foreach ($terms as $term) {
|
|
||||||
$term_data[$term->id()] = $term->label();
|
|
||||||
}
|
|
||||||
|
|
||||||
$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) {
|
|
||||||
$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
|
|
||||||
// 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 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
|
|
||||||
$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
|
// The below almost serve more as examples that can be followed
|
||||||
|
|
Reference in a new issue