Email us at info@henrytech.org to discuss your Drupal Migration today!
Sometimes during a migration, you need to do some processing on a field before migrating it. In this case you can write a source plugin. Here's how to do it for a node migration:
In your your_custom_module/src/Plugin/migrate/source/CustomPlugin.php file:
<?php
namespace Drupal\your_custom_module\Plugin\migrate\source;
use Drupal\node\Plugin\migrate\source\d7\Node;
use Drupal\migrate\Row;
use Drupal\user\Entity\User;
/**
* Custom node source plugin.
*
* @MigrateSource(
* id = "custom_plugin",
* source_module = "node"
* )
*/
class CustomNode extends Node {
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
// Grab field values from migration
$nid = $row->getSourceProperty('nid');
$node_type = $row->getSourceProperty('type');
// Query the old database
$query = $this->select('some_table_in_old_db', 'sfot')
->fields('sfot', ['some_field_in_old_table'])
->condition('another_field_in_old_table', $nid)
->condition('entity_type', 'node')
->execute()
->fetchAll();
// Do something with your data!
$new_value = $query;
// Set the property to use as source in the yaml.
$row->setSourceProperty('new_source_field', $new_value);
return parent::prepareRow($row);
}
}
Then you can referece the plugin in your migration script:
langcode: en
status: true
dependencies: { }
id: your_migration_id
class: Drupal\migrate\Plugin\Migration
migration_tags:
- 'Drupal 7'
migration_group: nodes
label: 'Nodes (your node type)'
source:
plugin: custom_plugin // This is what we defined above
node_type: your_node_type
process:
nid: nid
langcode:
plugin: default_value
source: language
default_value: und
title:
plugin: skip_on_empty // This is a useful plugin!
method: row
source: title
body:
plugin: sub_process
source: body
process:
value: value
summary: summary
format:
plugin: default_value
default_value: full_html // We often need to set the text format!
path/pathauto:
plugin: default_value
default_value: 0 // If you're using an alias
path/alias: alias
status: status
created: created
changed: changed
promote: promote
sticky: sticky
new_field: new_source_field
destination:
plugin: 'entity:node'
default_bundle: your_node_type
migration_dependencies: {}

Comments