Compare commits

...

3 commits

3 changed files with 39 additions and 26 deletions

View file

@ -1,5 +1,4 @@
name: Agaric Migration Helpers name: 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:
agaric_migration_helpers.field_transformations: 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\agaric_migration_helpers; namespace Drupal\migration_helpers;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
class AgaricMigrationHelperFieldTransformations { class MigrationHelperFieldTransformations {
/** /**
* The entity type manager. * The entity type manager.
@ -14,7 +14,7 @@ class AgaricMigrationHelperFieldTransformations {
protected $entityTypeManager; protected $entityTypeManager;
/** /**
* Constructs an AgaricMigrationHelperFieldTransformations object. * Constructs an MigrationHelperFieldTransformations object.
*/ */
public function __construct( public function __construct(
EntityTypeManagerInterface $entity_type_manager EntityTypeManagerInterface $entity_type_manager
@ -50,15 +50,30 @@ class AgaricMigrationHelperFieldTransformations {
foreach ($entities as $entity) { foreach ($entities as $entity) {
$media_target_ids = []; $media_target_ids = [];
$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());
}
}
}
foreach ($translations as $translation) {
foreach ($source_field_names as $source_field_name) { foreach ($source_field_names as $source_field_name) {
foreach ($entity->$source_field_name as $fieldItem) { foreach ($translation->$source_field_name as $fieldItem) {
$entity_data = [ $entity_data = [
'bundle' => $media_entity_bundle, 'bundle' => $media_entity_bundle,
'uid' => $entity->getOwnerId(), 'langcode' => $fieldItem->getLangcode(),
'langcode' => $entity->language()->getId(),
'status' => 1, '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) { foreach (\array_keys($fieldItem->getValue()) as $subfield) {
$entity_data[$media_target_field][$subfield] = $fieldItem->get($subfield)->getValue(); $entity_data[$media_target_field][$subfield] = $fieldItem->get($subfield)->getValue();
@ -69,10 +84,9 @@ class AgaricMigrationHelperFieldTransformations {
$media_target_ids[] = $media_entity->id(); $media_target_ids[] = $media_entity->id();
} }
} }
$translation->set($media_field_name, $media_target_ids);
$entity->set($media_field_name, $media_target_ids); }
$entity->save(); $entity->save();
} }
} }
} }