Compare commits

..

No commits in common. "050676f4bd8e52ebfca758b3c5dd30444ce0b6e9" and "f8fae5fc27c6fbfc0b6dd41735f317c5ceecbd28" have entirely different histories.

3 changed files with 25 additions and 38 deletions

View file

@ -1,4 +1,5 @@
name: Migration Helpers name: Agaric Migration Helpers
type: module type: module
description: Provides helpers to migrate data into modern Drupal. description: Provides helpers to migrate data into modern Drupal.
package: Agaric
core_version_requirement: ^9 || ^10 core_version_requirement: ^9 || ^10

View file

@ -1,4 +1,4 @@
services: services:
migration_helpers.field_transformations: agaric_migration_helpers.field_transformations:
class: Drupal\migration_helpers\MigrationHelperFieldTransformations class: Drupal\migration_helpers\MigrationHelperFieldTransformations
arguments: ['@entity_type.manager'] arguments: ['@entity_type.manager']

View file

@ -1,10 +1,10 @@
<?php <?php
namespace Drupal\migration_helpers; namespace Drupal\agaric_migration_helpers;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
class MigrationHelperFieldTransformations { class AgaricMigrationHelperFieldTransformations {
/** /**
* The entity type manager. * The entity type manager.
@ -14,7 +14,7 @@ class MigrationHelperFieldTransformations {
protected $entityTypeManager; protected $entityTypeManager;
/** /**
* Constructs an MigrationHelperFieldTransformations object. * Constructs an AgaricMigrationHelperFieldTransformations object.
*/ */
public function __construct( public function __construct(
EntityTypeManagerInterface $entity_type_manager EntityTypeManagerInterface $entity_type_manager
@ -50,43 +50,29 @@ class MigrationHelperFieldTransformations {
foreach ($entities as $entity) { foreach ($entities as $entity) {
$media_target_ids = []; $media_target_ids = [];
$langcodes = $entity->getTranslationLanguages();
$translations = []; foreach ($source_field_names as $source_field_name) {
if ($langcodes > 0) { foreach ($entity->$source_field_name as $fieldItem) {
foreach($langcodes as $langcode) { $entity_data = [
$check_translation = $entity->getTranslation($langcode->getId()); 'bundle' => $media_entity_bundle,
if ($check_translation !== NULL) { 'uid' => $entity->getOwnerId(),
$translations[] = $entity->getTranslation($langcode->getId()); '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();
} }
} }
foreach ($translations as $translation) { $entity->set($media_field_name, $media_target_ids);
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(); $entity->save();
} }
} }
} }