How to migrate content from drupal 6 to 7 by using Migrate_d2d - Part 4 - nodes

By steinmb, 2 June, 2015
Image

This series would be incomplete without also covering node migration. The example code is working code and should import data from drupal 6 to 7 if you have correctly configured your test setup by following the previously articles (part 1-3). Do not be scared by the code. It is more PHP defining stuff then "real" code containing a lot of logic and stuff.

### Blog post in this series

- [Part 1 - basic configuration/understanding, setup and user migration](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…).
- [Part 2 - Taxonomy migration](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…).
- [Part 3 - File migration](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…).
- [Part 4 - Node migration](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…).
- [Part 4-1 - Node migration - field mappings](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…)

# Migrating nodes from Drupal 6 to 7
Please read part 1, 2 and 3 for more information on registering classes and basic usage of migrate with drush.

## Drupal 6
You should already have created a set of nodes to migrate, if not, the easiest way of doing this is by using devel generate. I talk about this back in Part 1.

Drupal 6 story fields
Drupal 6 story fields.

Drupal 6 page fields contain no custom fields but have a taxonomy added
Drupal 6 page fields contain no custom fields but have a taxonomy added.

## Drupal 7
You should already have created the needed target fields back in Part 1.

Drupal 7 article fields
Drupal 7 target node type - article.

Drupal 7 page fields
Drupal 7 page fields.

# Example 4 - Migrate nodes from Drupal 6 to 7
Grab the code from github and clone it into your Drupal 7 sites/all/modules/ directory.

git clone --branch example4 https://github.com/steinmb/smb_migrate.git/

Or if you already have cloned it by following part 1-3, simply switch branch.

git checkout example4

Register your classes and output from migrate status (drush ms) should be something like this:

Group: content_migration Total Imported Unprocessed Status
User 51 0 0 Idle
Article 48 0 48 Idle
File 1601 0 1601 Idle
Page 52 0 52 Idle

Group: taxonomy Total Imported Unprocessed Status
Tags 14 0 14 Idle
Tags2 17 0 17 Idle
Tags3 19 0 19 Idle

Migrate your content as usual: drush mi Article and drush mi Page. Example output:

Processed 52 (52 created, 0 updated, 0 failed, 0 ignored) in 0.5 sec (5798/min) - done with 'Page'

This is actually all you need to get started migrating nodes. Notice that it by default does not migrate our taxonomy terms, files and the custom text field. You need to define a mapping for this to work. Telling migrate the name of the field to grab and the name to store it in. I'll talk more about that in a follow up article (4-1).

# Code breakdown
I will here only review the code that have been added/changed since part 3. Have you been following this series should this look very familiar to you.

### node.inc

* @file node.inc
* Base class for migrating nodes into Drupal.
*/

/**
* Common mappings for the Drupal 6 node migration.
*/
abstract class SMBNodeMigration extends DrupalNode6Migration {
public function __construct(array $arguments) {
parent::__construct($arguments);
}
}
?>

This class is used to store any settings and field mapping that are common for all nodes.

### article.inc

* @file article.inc
* Article node migration from drupal 6 story.
*/
class ArticleMigration extends SMBNodeMigration {
public function __construct(array $arguments) {
parent::__construct($arguments);
}
}
?>

### page.inc

* @file page.inc
* Page node migration from drupal 6 to 7.
*/
class PageMigration extends SMBNodeMigration {
public function __construct(array $arguments) {
parent::__construct($arguments);
}
}
?>

page.inc and article.inc contain the migration classes needed to import the two node content types (bundles) we` have. Notice they both extends the SMBNodeMigration class that is our common node migration class. Try to keep each class in a file by it self. Good practice and you are conforming to PSR-1 coding standard.

### smb_migrate.migrate.inc


* Node migrations - each has its own class derived from the migrate_d2d class,
* specifying its particular field mappings and transformations. source_type
* and destination_type are required arguments.
*/
$node_arguments = array(
'Page' => array(
'class_name' => 'PageMigration',
'description' => t('Migration of page nodes from Drupal 6'),
'source_type' => 'page',
'destination_type' => 'page',
),
'Article' => array(
'class_name' => 'ArticleMigration',
'description' => t('Migration of article nodes from Drupal 6'),
'source_type' => 'story',
'destination_type' => 'article',
),
);

/**
* Tell the node migrations where the users are coming from, so they can
* set up the dependency and resolve D6->D7 uids.
*/
$common_node_arguments = $common_arguments + array(
'user_migration' => 'User'
);

foreach ($node_arguments as $migration_name => $arguments) {
$arguments = array_merge_recursive($arguments, $common_node_arguments);
$api['migrations'][$migration_name] = $arguments;
}
?>

- class_name - The class name used in article.inc and page.inc.
- source_type - Drupal 6 content type machine name.
- destination_type - Drupal 7 content type machine name.
- 'user_migration' - Tell the node migrations where the users are coming from, so they can set up the dependency and resolve D6->D7 uids.

## Blog post in this series

- [Part 1 - basic configuration/understanding, setup and user migration](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…).
- [Part 2 - Taxonomy migration](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…).
- [Part 3 - File migration](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…).
- [Part 4 - Node migration](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…).
- [Part 4-1 - Node migration - field mappings](https://smbjorklund.no/how-migrate-content-drupal-6-7-using-migrated2d-…)