diff --git a/src/Plugin/migrate/source/d7/ParagraphsType.php b/src/Plugin/migrate/source/d7/ParagraphsType.php index 24953bd..17139ae 100644 --- a/src/Plugin/migrate/source/d7/ParagraphsType.php +++ b/src/Plugin/migrate/source/d7/ParagraphsType.php @@ -32,11 +32,18 @@ class ParagraphsType extends DrupalSqlBase { * {@inheritdoc} */ public function query() { - $query = $this->select('paragraphs_item', 'pi') + $query = $this->select('paragraphs_bundle', 'pt') + // We have to use distinct() here as well: countQuery wont work otherwise. ->distinct() - ->fields('pi', ['bundle']); - $query->leftJoin('paragraphs_bundle', 'pb', 'pb.bundle = pi.bundle'); - $query->fields('pb', ['name', 'locked']); + ->fields('pt'); + + $missing_bundles_from_items_query = $this->select('paragraphs_item', 'pt') + ->distinct() + ->fields('pt', ['bundle']) + ->fields('pb', ['name', 'locked']); + $missing_bundles_from_items_query->leftJoin('paragraphs_bundle', 'pb', 'pb.bundle = pt.bundle'); + + $query->union($missing_bundles_from_items_query); return $query; } @@ -47,7 +54,7 @@ class ParagraphsType extends DrupalSqlBase { // Paragraph bundles did not have descriptions in d7, optionally add one. if ($this->configuration['add_description']) { - $name = $row->getSourceProperty('name'); + $name = $row->getSourceProperty('name') ?? preg_replace('/_+/', ' ', $row->getSourceProperty('bundle')); $row->setSourceProperty('description', 'Migrated from paragraph bundle ' . $name); } else { @@ -73,7 +80,7 @@ class ParagraphsType extends DrupalSqlBase { */ public function getIds() { $ids['bundle']['type'] = 'string'; - $ids['bundle']['alias'] = 'pi'; + $ids['bundle']['alias'] = 'pt'; return $ids; } diff --git a/tests/src/Kernel/Plugin/migrate/source/ParagraphsTypeTest.php b/tests/src/Kernel/Plugin/migrate/source/ParagraphsTypeTest.php new file mode 100644 index 0000000..1516d00 --- /dev/null +++ b/tests/src/Kernel/Plugin/migrate/source/ParagraphsTypeTest.php @@ -0,0 +1,135 @@ + [ + [ + 'name' => 'paragraphs', + 'status' => 1, + ] + ], + 'paragraphs_bundle' => [ + [ + 'bundle' => 'bundle_one', + 'name' => 'Bundle One', + 'locked' => 1, + ], + [ + 'bundle' => 'bundle_two', + 'name' => 'Bundle Two', + 'locked' => 1, + ], + ], + 'paragraphs_item' => [ + [ + 'bundle' => 'bundle_one', + ], + ], + ]; + + return [ + 'Without missing items' => [ + 'Database' => [ + 'paragraphs_item' => [ + [ + 'bundle' => 'bundle_one', + ], + [ + 'bundle' => 'bundle_two', + ] + ], + ] + $default_db, + 'Expected results' => [ + [ + 'bundle' => 'bundle_one', + 'name' => 'Bundle One', + 'locked' => 1, + 'description' => '', + ], + [ + 'bundle' => 'bundle_two', + 'name' => 'Bundle Two', + 'locked' => 1, + 'description' => '', + ], + ], + ], + 'Bundles without instances, with description' => [ + 'Database' => $default_db, + 'Expected results' => [ + [ + 'bundle' => 'bundle_one', + 'name' => 'Bundle One', + 'locked' => 1, + 'description' => '', + ], + [ + 'bundle' => 'bundle_two', + 'name' => 'Bundle Two', + 'locked' => 1, + 'description' => '', + ], + ], + ], + 'Test with missing bundle' => [ + 'Database' => [ + 'paragraphs_item' => [ + [ + 'bundle' => 'bundle_two', + ], + [ + 'bundle' => 'missing_bundle', + ], + ], + ] + $default_db, + 'Expected results' => [ + [ + 'bundle' => 'bundle_one', + 'name' => 'Bundle One', + 'locked' => 1, + 'description' => 'Migrated from paragraph bundle Bundle One', + ], + [ + 'bundle' => 'bundle_two', + 'name' => 'Bundle Two', + 'locked' => 1, + 'description' => 'Migrated from paragraph bundle Bundle Two', + ], + [ + 'bundle' => 'missing_bundle', + 'name' => NULL, + 'locked' => NULL, + 'description' => 'Migrated from paragraph bundle missing bundle', + ], + ], + 'Expected count' => NULL, + 'Plugin config' => [ + 'add_description' => TRUE, + ], + ], + ]; + } + +}