Commit 7aed01cf authored by mikelutz's avatar mikelutz Committed by miro_dietiker

Issue #2911241 by mikelutz, Adita, dhruveshdtripathi, heddn: Migrate field...

Issue #2911241 by mikelutz, Adita, dhruveshdtripathi, heddn: Migrate field collections types as paragraph types
parent 06e75548
id: d7_field_collection_type
label: Paragraphs - Field Collection type configuration
migration_tags:
- Drupal 7
source:
plugin: d7_field_collection_type
prefix: field_
addDescription: true
process:
id: bundle
label: name
description: description
destination:
plugin: 'entity:paragraphs_type'
id: d7_paragraphs_type
label: Paragraphs type configuration
migration_tags:
- Drupal 7
source:
plugin: d7_paragraphs_type
addDescription: true
process:
id: bundle
label: name
description: description
destination:
plugin: 'entity:paragraphs_type'
<?php
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
/**
* Field_collection_type source.
*
* @MigrateSource(
* id = "d7_field_collection_type",
* source_module = "field_collection"
* )
*/
class FieldCollectionType extends DrupalSqlBase {
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('field_config', 'fc')
->fields('fc');
$query->condition('fc.type', 'field_collection');
$query->condition('fc.active', TRUE);
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$name = $row->getSourceProperty('field_name');
if ($this->configuration['addDescription']) {
$row->setSourceProperty('description', 'Migrated from field_collection ' . $name);
}
else {
$row->setSourceProperty('description', '');
}
// Remove prefix if it is set in configs.
$prefix = $this->configuration['prefix'];
if (isset($prefix) && strpos($name, $prefix) === 0) {
$name = substr($name, strlen($prefix));
}
$row->setSourceProperty('bundle', $name);
// Set label from bundle because we don't have a label in D7 field
// collections.
$row->setSourceProperty('name', ucfirst(preg_replace('/_/', ' ', $name)));
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
return [
'bundle' => $this->t('Paragraph type machine name'),
'name' => $this->t('Paragraph type label'),
'description' => $this->t('Paragraph type description'),
];
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['id']['type'] = 'integer';
return $ids;
}
}
<?php
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
/**
* Paragraphs source.
*
* @MigrateSource(
* id = "d7_paragraphs_type",
* source_module = "paragraphs"
* )
*/
class ParagraphType extends DrupalSqlBase {
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('paragraphs_bundle', 'pb')
->fields('pb');
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
if ($this->configuration['addDescription']) {
$name = $row->getSourceProperty('name');
$row->setSourceProperty('description', 'Migrated from paragraph bundle ' . $name);
}
else {
$row->setSourceProperty('description', '');
}
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
return [
'bundle' => $this->t('Paragraph type machine name'),
'name' => $this->t('Paragraph type label'),
'description' => $this->t('Paragraph type description'),
];
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['bundle']['type'] = 'string';
return $ids;
}
}
This diff is collapsed.
<?php
namespace Drupal\Tests\paragraphs\Kernel;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\Tests\migrate_drupal\Kernel\MigrateDrupalTestBase;
/**
* Tests Paragraph and Field Collection Migrations.
*
* @group paragraphs
*/
class ParagraphsMigrationTest extends MigrateDrupalTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['paragraphs', 'user', 'system', 'field'];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->loadFixture(__DIR__ . '/../../fixtures/drupal7.php');
$this->executeMigration('d7_field_collection_type');
$this->executeMigration('d7_paragraphs_type');
}
/**
* Check to see if paragraph types were created.
*
* @param string $bundle_machine_name
* The bundle to test for.
* @param string $bundle_label
* The bundle's label.
*/
protected function assertParagraphBundleExists($bundle_machine_name, $bundle_label) {
$bundle = ParagraphsType::load($bundle_machine_name);
$this->assertInstanceOf(ParagraphsType::class, $bundle);
$this->assertEquals($bundle_label, $bundle->label());
}
/**
* Test if the field collection type was brought over as a paragraph.
*/
public function testFieldCollectionTypeMigration() {
$this->assertParagraphBundleExists('field_collection_test', 'Field collection test');
}
/**
* Test if the paragraph type was brought over as a paragraph.
*/
public function testParagraphsTypeMigration() {
$this->assertParagraphBundleExists('paragraph_bundle_test', 'Paragraph Bundle Test');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment