1

"node_a_migration" is the migration ID of nodes of type "node_a"
"node_b_migration" is the migration ID of nodes of type "node_b"

I'd like to use either "node_a_migration" or "node_b_migration" as a migration_lookup depending on other field value.

The source (not a drupal site, entities of content A and content B can have the same id, because stored in different tables) :

[
  ['entity_type' => 'content_a', 'entity_id' => 123],
  ['entity_type' => 'content_b', 'entity_id' => 123],
]

The migration config :

process:
  pseudo_field_migration_id:
    plugin: static_map
    map:
      content_a: node_a_migration
      content_b: node_b_migration
    source: entity_type
  field_entity_reference:
    plugin: migration_lookup
    migration: '@pseudo_field_migration_id'
    source: entity_id

There is no error when running the migration, but the field_entity_reference is not populated.
It works if I manually replace '@pseudo_field_type_id' by node_a_migration

Any idea how to make it work ?

Thank you!

2
  • 2
    This is not possible in YML, the migration parameter is a config value and expects a static string. Only the source parameter can fetch dynamic values from the source row. You have to create your own process plugin to do something like this.
    – Hudri
    Commented Nov 6, 2020 at 13:36
  • Ok thank you i'll try to create my own process plugin and post my solution
    – DamienGR
    Commented Nov 7, 2020 at 9:27

1 Answer 1

1

As @Hudri suggested, I've created my own process plugin (extending MigrationLookup plugin):

my_module/src/Plugin/migrate/process/CustomMigrationLookup.php


namespace Drupal\my_module\Plugin\migrate\process;

use Drupal\migrate\Plugin\migrate\process\MigrationLookup;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;

/**
 * @MigrateProcessPlugin(
 *   id = "custom_migration_lookup"
 * )
 */
class CustomMigrationLookup extends MigrationLookup {
  
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    // We set the migration id based on the field configured in "migration_id_source"
    $this->configuration['migration'] = $row->getDestinationProperty($this->configuration['migration_id_source']);
    // We continue with the standard migration_lookup process
    return parent::transform($value, $migrate_executable, $row, $destination_property);
  }

}

The migration config :

process:
  pseudo_field_migration_id:
    plugin: static_map
    map:
      content_a: node_a_migration
      content_b: node_b_migration
    source: entity_type
  field_entity_reference:
    plugin: custom_migration_lookup
    migration_id_source: 'pseudo_field_migration_id'
    source: entity_id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.