Migrate A Paragraph Reference Field With Multiple Values

April 10, 2020 | 2 Minute Read

Tags: migration

At the start of 2020, Drupal released the 8.8 version with a stable Migrate module. You can refer to the detailed documentation page for the migration module to learn more about it. 

Recently, I worked on a product information management tool for a giant electronics company based out of Japan. The requirement was to migrate data from their old SQL-based platform to Drupal 8. There were hundreds of taxonomies, custom entities, nodes, and paragraphs that needed to be migrated to Drupal 8 as per the architecture. The Migrate module along with its sub-modules seemed to be the most suitable option for this task.

For one of the migrations, I had to transfer the data to a multi-valued paragraph reference field in the nodes. It seemed to be a difficult task as there’s a Drupal core issue due to which the migration_lookup only returns the first value. I used the prepareRow method provided by the Drupal SqlBase source plugin as a workaround for the issue. The prepareRow method helps you to modify the data row before it passes through any further methods.

This is a guide for Drupal developers who want to migrate and attach a multivalued paragraph reference field in the parent entity (in our case it is a node). This guide assumes you are familiar with the basics of Drupal migration.

In order to migrate a paragraph reference field, you will have to migrate the paragraph entities first. This tutorial does not cover the migration of the paragraph entity. We will be focusing on the next part, wherein we are attaching the imported paragraph entities to the multi-valued paragraph reference field of the parent entity. In order to import these, I’ve used the prepareRow method provided by the Drupal SqlBase source plugin. 

Please note that you’ll have to provide a unique ID as the composition of two columns, as that is going to be mapped in a multivalued field.

I’ve extended the SqlBase source plugin with the Helper class as given in the code provided above. 

Now, using the Helper function, we have made the content ready for the paragraph reference field and attached it using the setSourceProperty in the prepareRow (refer to the code above).

Note: The code is not complete; you’ll have to write all the required methods of the Sqlbase and you have to pass the actual values to the variables mentioned. You can refer to the Drupal documentation to migrate the data from a SQL source.

Below is the sample YAML file that you can refer to.

And with that, your migrate script is ready to import the multivalued paragraph reference field.