From 4ee8a3b4183ed347d7a47c6806185513c2a15a26 Mon Sep 17 00:00:00 2001 From: Keegan Rankin Date: Thu, 9 Nov 2023 17:41:57 -0800 Subject: [PATCH 1/2] Remove agaric from all names --- migration_helpers.info.yml | 3 +-- migration_helpers.services.yml | 2 +- src/MigrationHelperFieldTransformations.php | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/migration_helpers.info.yml b/migration_helpers.info.yml index bc7a6ca..7855059 100644 --- a/migration_helpers.info.yml +++ b/migration_helpers.info.yml @@ -1,5 +1,4 @@ -name: Agaric Migration Helpers +name: Migration Helpers type: module description: Provides helpers to migrate data into modern Drupal. -package: Agaric core_version_requirement: ^9 || ^10 diff --git a/migration_helpers.services.yml b/migration_helpers.services.yml index df7678d..a8a4d6d 100644 --- a/migration_helpers.services.yml +++ b/migration_helpers.services.yml @@ -1,4 +1,4 @@ services: - agaric_migration_helpers.field_transformations: + migration_helpers.field_transformations: class: Drupal\migration_helpers\MigrationHelperFieldTransformations arguments: ['@entity_type.manager'] diff --git a/src/MigrationHelperFieldTransformations.php b/src/MigrationHelperFieldTransformations.php index b10be49..e1ac020 100644 --- a/src/MigrationHelperFieldTransformations.php +++ b/src/MigrationHelperFieldTransformations.php @@ -1,10 +1,10 @@ Date: Thu, 9 Nov 2023 17:42:58 -0800 Subject: [PATCH 2/2] Refactor to account for migration of translated entities --- src/MigrationHelperFieldTransformations.php | 54 +++++++++++++-------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/MigrationHelperFieldTransformations.php b/src/MigrationHelperFieldTransformations.php index e1ac020..1f100d7 100644 --- a/src/MigrationHelperFieldTransformations.php +++ b/src/MigrationHelperFieldTransformations.php @@ -50,29 +50,43 @@ class MigrationHelperFieldTransformations { foreach ($entities as $entity) { $media_target_ids = []; - - foreach ($source_field_names as $source_field_name) { - foreach ($entity->$source_field_name as $fieldItem) { - $entity_data = [ - 'bundle' => $media_entity_bundle, - 'uid' => $entity->getOwnerId(), - 'langcode' => $entity->language()->getId(), - 'status' => 1, - ]; - - foreach (\array_keys($fieldItem->getValue()) as $subfield) { - $entity_data[$media_target_field][$subfield] = $fieldItem->get($subfield)->getValue(); + $langcodes = $entity->getTranslationLanguages(); + $translations = []; + if ($langcodes > 0) { + foreach($langcodes as $langcode) { + $check_translation = $entity->getTranslation($langcode->getId()); + if ($check_translation !== NULL) { + $translations[] = $entity->getTranslation($langcode->getId()); } - - $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); - $entity->save(); - } - } + foreach ($translations as $translation) { + 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(); + } + } } -- 2.45.2