I'm using json api feeds from another D8 site to migrate the data into new D8 site.

I setup an importer for taxonomy terms that is working fine. i.e. mycustom_migrator

I setup an importer for a custom entity that has an entity reference field to taxonomy terms (unlimited)

data example:


json api feeds...

"field_source_d8_terms": {
                    "data": [
                        {
                            "type": "taxonomy_term--mycustomtype",
                            "id": "189086b4-c59c-44bb-ad63-7bd2b9fceabc"
                        },
                        {
                            "type": "taxonomy_term--mycustomtype",
                            "id": "ce3486f4-19cb-4a76-a897-3dfc9440baf5"
                        }
                    ],

source...

      name: source_d8_terms
      label: 'D8 Terms'
      selector: /relationships/field_source_d8_terms

I don't know how to write the process plugin to iterate through the terms from the json feeds and import into the custom entity reference field. Please help.

field_mycustomentityfield:
    plugin: migration_lookup
    source: 'source_d8_terms/data/id'
    migration: mycustom_migrator

Comments

uceap-web created an issue.

yohky’s picture

Hello uceap-web,

Did you find a solution ... a year ago ?

I have exactly the same problem when trying to import D8 terms to another D8 via json.

Best

dpeamit’s picture

I had a slightly different use case as I was not migration from D8 to D8. I dint use the migration_lookup or entity_lookup (although you can use that as well). Here is what worked for me

#meetings is the entity ref field
field_meeting:
plugin: attach_meetings
source: Meetings

The source is a json , with multiple meetings objects (similar to yours). You can then iterate through through the array and attach that to the original entity (you can also check if it already attached to avoid attaching it multiple times)

<?php

namespace Drupal\pegaapi\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
 * Lookup an existing meeting entity.
 *
 * @MigrateProcessPlugin(
 *   id = "attach_meetings",
 * )
 */
class AttachMeetings extends ProcessPluginBase
{
    /**
     * {@inheritdoc}
     */
    public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty)
    {
        if (!empty($value)) {
            $entities = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
            'field_meeting_id' => $value,
             'type' => 'meeting',
            ]);

            if ($entities) {
                $node_ids = array();
                foreach ($entities as $entity) {
                    $node_ids[] = $entity->id();
                }

                return $node_ids;
            }
        }
    }
}
danflanagan8’s picture

I know this is way old, but this looks like something the sub_process plugin should be able to handle. When you have an array of arrays, you can use sub_process. It looks like source_d8_terms/data would be an array of arrays (where each array has an 'id' and a 'type'), so I think you could just do this:

field_mycustomentityfield:
    plugin: sub_process
    source: 'source_d8_terms/data'
    process:
      target_id:
        plugin: migration_lookup
        source: id
        migration: mycustom_migrator