From 989918023b2787b08211a208194e732fa032f924 Mon Sep 17 00:00:00 2001 From: Louis Elkner-Alfaro Date: Wed, 13 Sep 2023 11:02:56 -0700 Subject: [PATCH] init commit --- migration_helpers.info.yml | 5 ++ migration_helpers.services.yml | 4 ++ src/MigrationHelperFieldTransformations.php | 78 +++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 migration_helpers.info.yml create mode 100644 migration_helpers.services.yml create mode 100644 src/MigrationHelperFieldTransformations.php diff --git a/migration_helpers.info.yml b/migration_helpers.info.yml new file mode 100644 index 0000000..bc7a6ca --- /dev/null +++ b/migration_helpers.info.yml @@ -0,0 +1,5 @@ +name: Agaric 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 new file mode 100644 index 0000000..df7678d --- /dev/null +++ b/migration_helpers.services.yml @@ -0,0 +1,4 @@ +services: + agaric_migration_helpers.field_transformations: + class: Drupal\migration_helpers\MigrationHelperFieldTransformations + arguments: ['@entity_type.manager'] diff --git a/src/MigrationHelperFieldTransformations.php b/src/MigrationHelperFieldTransformations.php new file mode 100644 index 0000000..b10be49 --- /dev/null +++ b/src/MigrationHelperFieldTransformations.php @@ -0,0 +1,78 @@ +entityTypeManager = $entity_type_manager; + } + + /** + * Move data from file/image fields into media reference fields. + * + * Default transformation is image fields. + */ + public function fieldToMediaEntity($entity_type, $source_field_names, $media_field_name, $source_entity_bundles = NULL, $media_entity_bundle = 'image', $media_target_field = 'field_media_image') { + $media_entity_storage = $this->entityTypeManager->getStorage('media'); + $source_entity_storage = $this->entityTypeManager->getStorage($entity_type); + $entity_query = $source_entity_storage->getQuery(); + + $group = $entity_query->orConditionGroup(); + foreach ($source_field_names as $field) { + $group->exists($field); + } + $entity_query->condition($group); + + if (!\is_null($source_entity_bundles)) { + $bundle_key = $source_entity_storage->getEntityType()->getKey('bundle'); + $entity_query->condition($bundle_key, (array) $source_entity_bundles, 'IN'); + } + + $entity_query->accessCheck(FALSE); + $results = $entity_query->execute(); + + $entities = $source_entity_storage->loadMultiple($results); + + 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(); + } + + $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(); + } + } + +}