Skip to content
Field note

Migrate A Paragraph Reference Field With Multiple Values

Introduction

April 10, 2020·2 min read·Mehul Shah, PHP/Drupal Staff Engineer/ Field correspondent
Migrate A Paragraph Reference Field With Multiple Values

Introduction

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.

About the Project

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.

Companion brief

Bring this dispatch into a working session - one page in, scoping memo out.

Brief Foyer
Pressure-test "Field note" with a partner

If this dispatch landed, bring it into a room with us.

The argument in "Migrate A Paragraph Reference Field With Multiple Values" is ours - but every situation breaks the argument differently. Send the situation in one page and a named partner will reply inside one business day with a scoping memo: where the argument holds for you, where it bends, and what we'd actually do in the first two weeks.

One-pager · Field note

By submitting this form you acknowledge that you have read Axelerant's Privacy Policy and agree to its terms.

Replied to by a named partner within one business day.