Skip to content

Instantly share code, notes, and snippets.

@markdboyd
Created May 10, 2017 17:08
Show Gist options
  • Save markdboyd/e08c3de006f25ff43ed7fb2b1a280311 to your computer and use it in GitHub Desktop.
Save markdboyd/e08c3de006f25ff43ed7fb2b1a280311 to your computer and use it in GitHub Desktop.
Migrate D7 Field Collections to D8 Paragraphs
<?php
namespace Drupal\saintlukes_migrate\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
/**
* Drupal 7 field_collection_item source from database.
*
* @MigrateSource(
* id = "d7_field_collection_item",
* source_provider = "field_collection"
* )
*/
class FieldCollectionItem extends FieldableEntity {
/**
* {@inheritdoc}
*/
public function query() {
// Select node in its last revision.
$query = $this->select('field_collection_item', 'fci')
->fields('fci', [
'item_id',
'revision_id',
'field_name',
'archived',
]);
if (isset($this->configuration['field_name'])) {
$query->innerJoin('field_data_' . $this->configuration['field_name'], 'fd', 'fd.' . $this->configuration['field_name'] . '_value = fci.item_id');
$query->fields('fd', [
'entity_type',
'bundle',
'entity_id',
$this->configuration['field_name'] . '_revision_id',
]);
$query->condition('fci.field_name', $this->configuration['field_name']);
}
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
// If field specified, get field revision ID so there aren't issues mapping.
if (isset($this->configuration['field_name'])) {
$row->setSourceProperty('revision_id', $row->getSourceProperty($this->configuration['field_name'] . '_revision_id'));
}
// Get Field API field values.
foreach (array_keys($this->getFields('field_collection_item', $row->getSourceProperty('field_name'))) as $field) {
$item_id = $row->getSourceProperty('item_id');
$revision_id = $row->getSourceProperty('revision_id');
$row->setSourceProperty($field, $this->getFieldValues('field_collection_item', $field, $item_id, $revision_id));
}
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'item_id' => $this->t('Item ID'),
'revision_id' => $this->t('Revision ID'),
'field_name' => $this->t('Name of field'),
'entity_type' => $this->t('Host entity type'),
'entity_id' => $this->t('Host entity ID'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['item_id']['type'] = 'integer';
$ids['item_id']['alias'] = 'fci';
return $ids;
}
}
id: d7_field_collection_FIELD_NAME
label: D7 Field Collection - Field Name
source:
plugin: d7_field_collection_item
field_name: FIELD_COLLECTION_FIELD_NAME
process:
# Map fields from field collection to paragraph
field_example: field_example
type:
plugin: default_value
default_value: PARAGRAPH_TYPE
revision_id: revision_id
destination:
plugin: entity:paragraph
id: d7_node_NODE_TYPE
label: D7 Node - Node Type
source:
plugin: d7_node
node_type: D7_NODE_TYPE
process:
PARAGRAPH_FIELD:
plugin: iterator
source: FIELD_COLLECTION_FIELD_NAME
process:
target_id:
plugin: migration
migration: d7_field_collection_FIELD_NAME
source: value
target_revision_id: revision_id
destination:
plugin: entity:node
default_bundle: NODE_BUNDLE
migration_dependencies:
required:
- d7_field_collection_FIELD_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment