Refactor to account for migration of translated entities

This commit is contained in:
Keegan Rankin 2023-11-09 17:42:58 -08:00
parent 4ee8a3b418
commit 050676f4bd

View file

@ -50,29 +50,43 @@ class MigrationHelperFieldTransformations {
foreach ($entities as $entity) { foreach ($entities as $entity) {
$media_target_ids = []; $media_target_ids = [];
$langcodes = $entity->getTranslationLanguages();
foreach ($source_field_names as $source_field_name) { $translations = [];
foreach ($entity->$source_field_name as $fieldItem) { if ($langcodes > 0) {
$entity_data = [ foreach($langcodes as $langcode) {
'bundle' => $media_entity_bundle, $check_translation = $entity->getTranslation($langcode->getId());
'uid' => $entity->getOwnerId(), if ($check_translation !== NULL) {
'langcode' => $entity->language()->getId(), $translations[] = $entity->getTranslation($langcode->getId());
'status' => 1,
];
foreach (\array_keys($fieldItem->getValue()) as $subfield) {
$entity_data[$media_target_field][$subfield] = $fieldItem->get($subfield)->getValue();
} }
$media_entity = $media_entity_storage->create($entity_data);
$media_entity->save();
$media_target_ids[] = $media_entity->id();
} }
} }
$entity->set($media_field_name, $media_target_ids); foreach ($translations as $translation) {
$entity->save();
}
}
foreach ($source_field_names as $source_field_name) {
foreach ($translation->$source_field_name as $fieldItem) {
$entity_data = [
'bundle' => $media_entity_bundle,
'langcode' => $fieldItem->getLangcode(),
'status' => 1,
];
// Some entities (like taxonomy terms) do not have a an owner id
if(method_exists($entity, 'getOwnerId')) {
$entity_data['uid'] = $entity->getOwnerId();
}
foreach (\array_keys($fieldItem->getValue()) as $subfield) {
$entity_data[$media_target_field][$subfield] = $fieldItem->get($subfield)->getValue();
}
$media_entity = $media_entity_storage->create($entity_data);
$media_entity->save();
$media_target_ids[] = $media_entity->id();
}
}
$translation->set($media_field_name, $media_target_ids);
}
$entity->save();
}
}
} }