Commit d61ac951 authored by mikelutz's avatar mikelutz Committed by miro_dietiker

Issue #2911242 by mikelutz, citlacom, heddn: Create field collections source migrate plugin

parent bbd879e1
......@@ -4,8 +4,7 @@ migration_tags:
- Drupal 7
source:
plugin: d7_field_collection_type
prefix: field_
addDescription: true
add_description: true
process:
id: bundle
label: name
......
......@@ -4,7 +4,7 @@ migration_tags:
- Drupal 7
source:
plugin: d7_paragraphs_type
addDescription: true
add_description: true
process:
id: bundle
label: name
......
<?php
namespace Drupal\paragraphs\Plugin\migrate\source;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase as MigrateDrupalSqlBase;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\Plugin\MigrationInterface;
/**
* Base Class for Paragraphs DrupalSqlBase migrate source plugins.
*
* Add and implement Configurable Plugin interface to
* Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase.
*/
abstract class DrupalSqlBase extends MigrateDrupalSqlBase implements ConfigurablePluginInterface {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
$this->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this->defaultConfiguration(), $configuration);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
}
<?php
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
/**
* Field Collection Item source plugin.
*
* Available configuration keys:
* - field_name: (optional) If supplied, this will only return field collections
* of that particular type.
*
* @MigrateSource(
* id = "d7_field_collection_item",
* source_module = "field_collection",
* )
*/
class FieldCollectionItem extends FieldableEntity {
/**
* Join string for getting current revisions.
*/
const JOIN = 'f.revision_id = fr.revision_id';
/**
* Length of the 'field_' prefix that field collection prepends to bundles.
*/
const FIELD_COLLECTION_PREFIX_LENGTH = 6;
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'field_name' => '',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('field_collection_item', 'f')
->fields('f', [
'item_id',
'field_name',
'archived',
])
->fields('fr', ['revision_id']);
$query->innerJoin('field_collection_item_revision', 'fr', static::JOIN);
// This configuration item may be set by a deriver to restrict the
// bundles retrieved.
if ($this->configuration['field_name']) {
$query->condition('f.field_name', $this->configuration['field_name']);
}
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
parent::prepareRow($row);
// Remove field_ prefix for new bundle.
$bundle = $row->getSourceProperty('field_name');
$bundle = substr($bundle, static::FIELD_COLLECTION_PREFIX_LENGTH);
$row->setSourceProperty('bundle', $bundle);
// Get Field API field values.
$field_names = array_keys($this->getFields('field_collection_item', $row->getSourceProperty('field_name')));
$item_id = $row->getSourceProperty('item_id');
$revision_id = $row->getSourceProperty('revision_id');
foreach ($field_names as $field_name) {
$value = $this->getFieldValues('field_collection_item', $field_name, $item_id, $revision_id);
$row->setSourceProperty($field_name, $value);
}
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'item_id' => $this->t('The field_collection_item id'),
'revision_id' => $this->t('The field_collection_item revision id'),
'bundle' => $this->t('The field_collection bundle'),
'field_name' => $this - t('The field_collection field_name'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids = [
'item_id' => [
'type' => 'integer',
'alias' => 'f',
],
];
return $ids;
}
}
<?php
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
/**
* Field Collection Item Revision source plugin.
*
* Available configuration keys:
* - field_name: (optional) If supplied, this will only return field collections
* of that particular type.
*
* @MigrateSource(
* id = "d7_field_collection_item_revision",
* source_module = "field_collection",
* )
*/
class FieldCollectionItemRevision extends FieldCollectionItem {
/**
* Join string for getting all except the current revisions.
*/
const JOIN = "f.item_id = fr.item_id AND f.revision_id <> fr.revision_id";
/**
* {@inheritdoc}
*/
public function getIds() {
$ids = [
'revision_id' => [
'type' => 'integer',
'alias' => 'fr',
],
];
return $ids;
}
}
......@@ -3,10 +3,14 @@
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\paragraphs\Plugin\migrate\source\DrupalSqlBase;
/**
* Field_collection_type source.
* Field Collection Type source plugin.
*
* Available configuration keys:
* - add_description: (bool) (optional) If enabled this will add a default
* description to the source data. default:FALSE.
*
* @MigrateSource(
* id = "d7_field_collection_type",
......@@ -15,6 +19,20 @@ use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
*/
class FieldCollectionType extends DrupalSqlBase {
/**
* Length of the 'field_' prefix that field collection prepends to bundles.
*/
const FIELD_COLLECTION_PREFIX_LENGTH = 6;
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'add_description' => FALSE,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
......@@ -33,22 +51,21 @@ class FieldCollectionType extends DrupalSqlBase {
$name = $row->getSourceProperty('field_name');
if ($this->configuration['addDescription']) {
// Remove field_ prefix for new bundle.
$bundle = substr($name, static::FIELD_COLLECTION_PREFIX_LENGTH);
$row->setSourceProperty('bundle', $bundle);
// Field collections don't have descriptions, optionally add one.
if ($this->configuration['add_description']) {
$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)));
$row->setSourceProperty('name', ucfirst(preg_replace('/_/', ' ', $bundle)));
return parent::prepareRow($row);
}
......@@ -58,6 +75,7 @@ class FieldCollectionType extends DrupalSqlBase {
*/
public function fields() {
return [
'field_name' => $this->t('Original field collection bundle/field_name'),
'bundle' => $this->t('Paragraph type machine name'),
'name' => $this->t('Paragraph type label'),
'description' => $this->t('Paragraph type description'),
......@@ -68,8 +86,32 @@ class FieldCollectionType extends DrupalSqlBase {
* {@inheritdoc}
*/
public function getIds() {
$ids['id']['type'] = 'integer';
$ids['field_name']['type'] = 'string';
return $ids;
}
/**
* {@inheritdoc}
*
* @deprecated addDescription configuration key is deprecated in 8.x-1.0 and
* will be removed prior to 9.x. Use add_description instead.
*
* @see https://www.drupal.org/project/paragraphs/issues/2911242
*/
public function setConfiguration(array $configuration) {
// Configuration keys should be snake_case, not CamelCase. Adding a BC
// layer for addDescription which should be deprecated.
if (isset($configuration['addDescription'])) {
@trigger_error("addDescription configuration key is deprecated in 8.x-1.0 and will be removed prior to 9.x. Use add_description instead. See https://www.drupal.org/project/paragraphs/issues/2911242", E_USER_DEPRECATED);
if (!isset($configuration['add_description'])) {
$configuration['add_description'] = $configuration['addDescription'];
}
unset($configuration['addDescription']);
}
parent::setConfiguration($configuration);
}
}
<?php
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity as MigrateFieldableEntity;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\Plugin\MigrationInterface;
/**
* Base Class for Paragraphs FieldableEntity migrate source plugins.
*
* Add and implement Configurable Plugin interface to
* Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity.
*/
abstract class FieldableEntity extends MigrateFieldableEntity implements ConfigurablePluginInterface {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
$this->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this->defaultConfiguration(), $configuration);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
}
......@@ -2,62 +2,17 @@
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
@trigger_error('\Drupal\paragraphs\Plugin\migrate\source\d7\ParagraphType is deprecated in Paragraphs 8.x-1.0 and will be removed before 9.x use
* \Drupal\paragraphs\Plugin\migrate\source\d7\ParagraphsType instead. See https://www.drupal.org/project/paragraphs/issues/2911242', E_USER_DEPRECATED);
/**
* Paragraphs source.
* Paragraphs Type source plugin.
*
* @MigrateSource(
* id = "d7_paragraphs_type",
* source_module = "paragraphs"
* )
* @deprecated in Paragraphs 8.x-1.0 and will be removed before 9.x use
* \Drupal\paragraphs\Plugin\migrate\source\d7\ParagraphsType instead.
*
* @see https://www.drupal.org/project/paragraphs/issues/2911242
*/
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;
}
class ParagraphType extends ParagraphsType {
}
<?php
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
/**
* Paragraphs Item source plugin.
*
* Available configuration keys:
* - bundle: (optional) If supplied, this will only return paragraphs
* of that particular type.
*
* @MigrateSource(
* id = "d7_paragraphs_item",
* source_module = "paragraphs",
* )
*/
class ParagraphsItem extends FieldableEntity {
/**
* Join string for getting current revisions.
*/
const JOIN = "p.revision_id = pr.revision_id";
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'bundle' => '',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('paragraphs_item', 'p')
->fields('p',
['item_id',
'bundle',
'field_name',
'archived',
])
->fields('pr', ['revision_id']);
$query->innerJoin('paragraphs_item_revision', 'pr', static::JOIN);
// This configuration item may be set by a deriver to restrict the
// bundles retrieved.
if ($this->configuration['bundle']) {
$query->condition('p.bundle', $this->configuration['bundle']);
}
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
// Get Field API field values.
$item_id = $row->getSourceProperty('item_id');
$revision_id = $row->getSourceProperty('revision_id');
foreach (array_keys($this->getFields('paragraphs_item', $row->getSourceProperty('bundle'))) as $field) {
$row->setSourceProperty($field, $this->getFieldValues('paragraphs_item', $field, $item_id, $revision_id));
}
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'item_id' => $this->t('The paragraph_item id'),
'revision_id' => $this->t('The paragraph_item revision id'),
'bundle' => $this->t('The paragraph bundle'),
'field_name' => $this - t('The paragrpah field_name'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids = [
'item_id' => [
'type' => 'integer',
'alias' => 'p',
],
];
return $ids;
}
}
<?php
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
/**
* Paragraphs Item Revision source plugin.
*
* Available configuration keys:
* - bundle: (optional) If supplied, this will only return paragraphs
* of that particular type.
*
* @MigrateSource(
* id = "d7_paragraphs_item_revision",
* source_module = "paragraphs",
* )
*/
class ParagraphsItemRevision extends ParagraphsItem {
/**
* Join string for getting all except the current revisions.
*/
const JOIN = "p.item_id=pr.item_id AND p.revision_id <> pr.revision_id";
/**
* {@inheritdoc}
*/
public function getIds() {
$ids = [
'revision_id' => [
'type' => 'integer',
'alias' => 'pr',
],
];
return $ids;
}
}
<?php
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\paragraphs\Plugin\migrate\source\DrupalSqlBase;
/**
* Paragraphs Type source plugin.
*
* Available configuration keys:
* - add_description: (bool) (optional) If enabled this will add a default
* description to the source data. default:FALSE.
*
* @MigrateSource(
* id = "d7_paragraphs_type",
* source_module = "paragraphs"
* )
*/
class ParagraphsType extends DrupalSqlBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'add_description' => FALSE,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('paragraphs_bundle', 'pb')
->fields('pb');
return $query;