hi,

I am tired to migrate csv data into paragraphs type which should be attached to aNode. I am trying this from two weeks but no luck.

I have tried so many ymls but none of them working for multiple paragrpahs.

My csv sheets are like :

id,paraid,Title,Author
1,1,Revolution 2020,Chetan Bhagat
2,1,Revolution 2022,Chetan Bhagat2
3,2,Concept of Physics,H C Verma
4,2,Concept of Physics2,H C Verma2

id,First_Name,Last_Name,para_id
1,pankaj,sh,"1,2"
2,pranay,kum,"3,4"

Tried YMLs:

for Paragraph:

id: mbe_book_paragharphs9
migration_group: mbe_migrations
label: 'Import book paragraph'
source:
plugin: csv
path: '/var/www/html/drupal-8.8/author.csv'
delimiter: ','
enclosure: '"'
header_row_count: 1
ids: [id]
fields:
0:
name: id
label: 'Unique Id'
1:
name: Title
label: 'Title'
2:
name: Author
label: 'Author'
3:
name: paraid
label: 'paraid'
process:
field_mbe_title: Title
field_mbe_author: Author
field_para_id: paraid
destination:
plugin: 'entity_reference_revisions:paragraph'
default_bundle: mbe_book

For Node:

id: mbe_professors9
migration_group: mbe_migrations
label: 'Import professors'
source:
plugin: csv
path: '/var/www/html/drupal/prof.csv'
delimiter: ','
enclosure: '"'
header_row_count: 1
ids: [id]
process:
type:
plugin: default_value
default_value: mbe_professor
title:
plugin: concat
source:
- 'First_Name'
- 'Last_Name'
delimiter: ' '
field_mbe_favorite_books:
plugin: migration_lookup
migration: mbe_book_paragharphs9
allow_multiple: true
no_stub: true
source: paraid
plugin: iterator
process:
target_id: '0'
target_revision_id: '1'
destination:
plugin: 'entity:node'

Above not create single Pragraph but below YMLs will create at least one Pragraph:

Pragraph:

id: mbe_book_paragharphs3
migration_group: mbe_migrations
label: 'Import book paragraph'
source:
plugin: csv
path: '/var/www/html/drupal-8.8/author.csv'
delimiter: ','
enclosure: '"'
header_row_count: 1
ids: [id]
fields:
0:
name: id
label: 'Unique Id'
1:
name: Title
label: 'Title'
2:
name: Author
label: 'Author'
process:
field_mbe_title: Title
field_mbe_author: Author
destination:
plugin: 'entity_reference_revisions:paragraph'
default_bundle: mbe_book

Node:

id: mbe_professors3
migration_group: mbe_migrations
label: 'Import professors'
source:
plugin: csv
path: '/var/www/html/drupal/prof.csv'
delimiter: ','
enclosure: '"'
header_row_count: 1
ids: [id]
process:
type:
plugin: default_value
default_value: mbe_professor
title:
plugin: concat
source:
- 'First_Name'
- 'Last_Name'
delimiter: ' '
mbe_book_paragraphs:
plugin: migration_lookup
migration: mbe_book_paragharphs3
source: id
field_mbe_favorite_books:
plugin: sub_process
source:
- '@mbe_book_paragraphs'
process:
target_id: '0'
target_revision_id: '1'
destination:
plugin: 'entity:node'

Comments

pankajsharma456 created an issue.

y.elmimouni’s picture

the migration_lookup get the para_id as a string, what I suggest is to create a custom plugin process and implement the transform method.
here's what i did and probably fix your problem:

 field_field_mbe_favorite_books:
    plugin: custom_process
    source: para_id
    process:
      target_id: target_id
      target_revision_id: target_revision_id
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $para_id = explode(',', $value);
    $paragraph = Paragraph::loadMultiple($para_id);
    $paragraphs = [];
    foreach ($paragraph as $item) {
      if ($item instanceof ParagraphInterface) {
        $paragraphs[] = [
          'target_id' => $item->id(),
          'target_revision_id' => $item->getRevisionId()
        ];
      }
    }
    return $paragraphs;
  }