Migrate Process: Importing Multiple Paragraphs

Created on 7 November 2016, about 8 years ago
Updated 11 June 2023, over 1 year ago

OK, I tried working at this one and couldn't figure out the solution, so I'm writing up a request.

I'm trying to import Paragraphs using Migrate Source CSV. I've managed to get single paragraphs to import using Migrate.

However when I attempt to import multiple paragraphs using the explode plugin - everything seems to break.

Here's my migrate YML:

uuid: 88b8d4b8-0887-4dbf-884e-1d2483c3f2c0
id: import_content_test_1
label: Import Content Test 1

source:
  plugin: csv
  path: 'private://import_csv/content_test_1.csv'
  header_row_count: 1
  keys:
    - id
  column_names:
    -
      id: id
    - 
      title: title
    - 
      body: body
    -
      paragraph_test_1: paragraph_test_1
process:
  title: title
  body: body
  'field_paragraph_test_1/target_id':
    -
      plugin: explode
      source: paragraph_test_1
      delimiter: ,
    -
      plugin: migration
      migration: import_paragraph_test_1
      no_stub: true
    -
      plugin: extract
      index:
        - 0
  'field_paragraph_test_1/target_revision_id':
    -
      plugin: explode
      source: paragraph_test_1
      delimiter: ,
    -
      plugin: migration
      migration: import_paragraph_test_1
      no_stub: true
    -
      plugin: extract
      index:
        - 1
  type:
    plugin: default_value
    default_value: content_test_1
destination:
  plugin: entity:node

My CSV file has a row, with multiple values inside of it, e.g, "11,12", as follows:

id,title,body,paragraph_test_1
1,Title 1,Body 1,"11,12"
2,Title 2,Body 2,"21,22"

When I run the import, it fails, and generates the error:

Value is not a valid entity.                                                                            [error]
(***/modules/entity_reference_revisions/src/Plugin/DataType/EntityReferenceRevisions.php:114)

If I dig into what EntityReferenceRevisions.php is expecting around line 114:

  public function setValue($value, $notify = TRUE) {
    unset($this->target);
    unset($this->id);
    unset($this->revision_id);
  
    // Both the entity ID and the entity object may be passed as value. The
    // reference may also be unset by passing NULL as value.
    if (!isset($value)) {
      $this->target = NULL;
    }
    elseif (is_object($value) && $value instanceof EntityInterface) {
      $this->target = $value->getTypedData();
    }
    elseif (!is_scalar($value['target_id']) || !is_scalar($value['target_revision_id']) || $this->getTargetDefinition()->getEntityTypeId() === NULL) {
      throw new \InvalidArgumentException('Value is not a valid entity.');
    }

EntityReferenceRevisions::setValue taking in the following as $value:

Array
(
    [target_id] => Array
        (
            [0] => 54
            [1] => 56
        )

    [target_revision_id] => Array
        (
            [0] => 55
            [1] => 57
        )

)

This seems wrong to me.. presumably this should be an Array of two numbered objects, each containing an array with keys target_id, target_revision_id. I think *should* look closer to this:

Array
(
    [0] => Array
        (
            [target_id] => 54
            [target_revision_id] => 56
        )

    [1] => Array
        (
            [target_id] => 55
            [target_revision_id] => 57
        )

)

I'm guessing my YML file isn't quite what it should be. I've tried several iterations, usually attempting to put the explode before the breakdown into target_id/target_revision_id, something along the lines of the following (non working) YML code:

uuid: 88b8d4b8-0887-4dbf-884e-1d2483c3f2c0
id: import_content_test_1
label: Import Content Test 1

source:
  plugin: csv
  path: 'private://import_csv/content_test_1.csv'
  header_row_count: 1
  keys:
    - id
  column_names:
    -
      id: id
    - 
      title: title
    - 
      body: body
    -
      paragraph_test_1: paragraph_test_1
process:
  title: title
  body: body
  field_paragraph_test_1:
    plugin: explode
    source: paragraph_test_1
    delimiter: ,
    process:
      target_id:
        -
          plugin: migration
          migration: import_paragraph_test_1
          no_stub: true
        -
          plugin: extract
          index:
            - 0
      target_revision_id:
        -<ol>
  <li></li>
</ol>
          plugin: migration
          migration: import_paragraph_test_1
          no_stub: true
        -
          plugin: extract
          index:
            - 1
  type:
    plugin: default_value
    default_value: content_test_1
destination:
  plugin: entity:node

I can't quite figure this out (the sub-process isn't sure of its source?)

Any pointers would be helpful. Thanks in advance!

πŸ’¬ Support request
Status

Closed: duplicate

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada TrevorBradley

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States inversed

    If you only need a default paragraph created, there is a module to add this functionality: https://www.drupal.org/project/migration_tools β†’

    This is by far the quickest way to generate a paragraph entity during the migration process.

    Here is their example YAML:

      field_some_paragraph_entity_ref_revison:
        plugin: create_default_paragraph_revision
        paragraph_default:
          create_paragraph_bundle: paint_recommendation
          field_color: blue
          field_paint_type: latex
          field_coats: 2
          field_exterior_use: true
    
Production build 0.71.5 2024