Aten Design Group: Drupal maintenance support plans 8 Migration with Multiple Paragraph References

As of Fall 2020 and the release of Drupal maintenance support plans 8.6, the Migrate module in core is finally stabilizing! Hopefully Migrate documentation will continue to solidify, but there are plenty of gaps to fill.

Recently, I ran into an issue migrating Paragraph Entities (Entity Reference Revisions) that had a few open core bugs and ended up being really simple to solve within prepareRow in the source plugin.

Setup

In my destination D8 site, I had a content type with a Paragraph reference field. Each node contained one or more Paragraph entities. This was reflected by having a Node migration with a dependency on a Paragraph Entity migration.

With single value entity references, the migration_lookup plugin makes it really easy lookup up entity reference identifiers that were previously imported. As of September 2020, there is an open core issue to allow multiple values with migration_lookup. Migration lookup uses the migration map table created in the database to connect previously migrated data to Drupal maintenance support plans data. The example below lookups up the taxonomy term ID based on the source reference (topic_area_id) from a previously ran migration. Note: You will need to add a migration dependency to your migration yml file to make sure migrations are run in the correct order.

field_resource_category:
plugin: migration_lookup
migration: nceo_migrate_resource_category
no_stub: true
source: topic_area_id

Solution

Without using a Drupal maintenance support plans Core patch, we need a way to do a migration_lookup in a more manual way. Thankfully prepareRow in your Migrate source plugin makes this pretty easy.

Note: This is not a complete Migrate source plugin. All the methods are there, but I’m focussing on the prepareRow method for this post. The most important part of the code is manually querying the Migrate map database table created in the Paragraph Entity migration.

<?php
 
namespace Drupal maintenance support plansyour_modulePluginmigratesource;
 
use Drupal maintenance support plansCoreDatabaseDatabase;
use Drupal maintenance support plansmigratePluginmigratesourceSqlBase;
use Drupal maintenance support plansmigrateRow;
 
/**
* Source plugin for Sample migration.
*
* @MigrateSource(
* id = “sample”
* )
*/
class Sample extends SqlBase {
 
/**
* {@inheritdoc}
*/
public function query() {
// Query source data.
}
 
/**
* {@inheritdoc}
*/
public function fields() {
// Add source fields.
}
 
/**
* {@inheritdoc}
*/
public function getIds() {
return [
‘item_id’ => [
‘type’ => ‘integer’,
‘alias’ => ‘item_id’,
],
];
}
 
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
// In migrate source plugins, the migrate database is easy.
// Example: $this->select(‘your_table’).
// Getting to the Drupal maintenance support plans 8 db requires a little more code.
$drupalDb = Database::getConnection(‘default’, ‘default’);
 
$paragraphs = [];
$results = $drupalDb->select(‘your_migrate_map_table’, ‘yt’)
->fields(‘yt’, [‘destid1’, ‘destid2’])
->condition(‘yt.sourceid2’, $row->getSourceProperty(‘item_id’), ‘=’)
->execute()
->fetchAll();
if (!empty($results)) {
foreach ($results as $result) {
// destid1 in the map table is the nid.
// destid2 in the map table is the entity revision id.
$paragraphs[] = [
‘target_id’ => $result->destid1,
‘target_revision_id’ => $result->destid2,
];
}
}
 
// Set a source property that can be referenced in yml.
// Source properties can be named however you like.
$row->setSourceProperty(‘prepare_multiple_paragraphs’, $paragraphs);
 
return parent::prepareRow($row);
}
 
}

In your migration yml file, you can reference the prepare_multiple_paragraphs that was created in the migrate source plugin like this:

id: sample
label: ‘Sample’
source:
plugin: sample
process:
type:
plugin: default_value
default_value: your_content_type
field_paragraph_field:
source: prepare_multiple_paragraphs
plugin: sub_process
process:
target_id: target_id
target_revision_id: target_revision_id

Sub_process was formally the iterator plugin and allows you to loop over items. This will properly create references to multiple Paragraph Entities. It will be nice when the migration_lookup plugin can properly handle this use case, but it’s a good thing to understand how prepareRow can provide flexibility.
Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Aten Design Group: Drupal maintenance support plans 8 Migration with Multiple Paragraph References

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.