Drupal8 Migrate API, Migrating Nodes From Drupal7 – Part 2

Please See: Drupal8 Migrate API, Migrating Nodes From Drupal7 – Part 1 for Part-1 of this series. This is the continuation of the same.

Earlier in the series, we went through the process of configuration of the migration source and how a migration module can be written. We also configured the migration definition and source plugin for the nodes that were to be migrated. The migration is dependent of two other migrations: migrate_fruit_vitamins and migrate_fruit_images. Let’s us walk-through their implementations.

2.4 Migrating Taxonomy Terms

Let us create a file /config/install/migrate_plus.migration.migrate_fruit_vitamins.yml with the following content, which is the migration definition for terms that we are going to import from the source instance. The default migration template for taxonomy terms is found at /core/modules/taxonomy/migration_templates/d7_taxonomy_term.yml.

Create a file \src\Plugin\migrate\source\Vitamins.php inside the module and add the following content to it. This will serve as the source plugin for the terms to be migrated. Let us extend the class Drupal\migrate\Plugin\migrate\source\SqlBase to write our own source plugin class for the terms. The particular methods that will be overridden are:

query() defines the basic query used to retrieve data from the source table.

fields() contains an array of the base fields within the source table.

getIds() Defines the source fields uniquely identifying a source row.

prepareRow() Adds additional data to the row. Any property we create using $row->setSourceProperty() will be available in the “process” step.

2.5 Migrating Images

Create a file /config/install/migrate_plus.migration.migrate_fruit_images.yml inside the module with the following content, which would be the migration definition for images to be migrated.

Create another file \src\Plugin\migrate\source\FruitImages.php and with the following content. This is the source plugin class for migration of the images. Here, the base plugin class Drupal\file\Plugin\migrate\source\d7\File is extended to implement the plugin.

In the last step, create a fruit_migration.install file and implement the hook_uninstall function to remove the migration definitions upon uninstalling the module from the database.

Voila! We have successfully created our migration module. The structure of the module should look like the one below:

3. Running the migration

Once the module is successfully enabled, navigate to the drupal8(destination) installation directory in the command prompt and execute the command drush migrate-status. This will list all the migrations available and their statuses.

Run the command drush migrate-import –group=fruit from the command prompt to process all the migrations in the group fruit and import the corresponding data from the source.

Migrations can be run individually using the command drush migrate-import <migration_name>. More information about the Drush migrate commands can be found here

Happy Migrating!!

About The Author

Malay Nayak

Acquia Certified Drupal8 Developer

Leave a Reply