Drupal 8 Why & How to Migrate - Part1


As I was working with the migration towards Drupal 8 scenarios, I was pondered with this question of why are we migrating to Drupal 8 and how to go about it. So, I decided to go ahead and share a few case scenarios which will be helpful for you in case you are looking forward to gather info on Drupal 8 migration. This guide will be useful who are looking for a basic idea of Drupal 8 & migrating to the same.

We will look into migration of different entities in the upcoming articles. In this article I will be explaining about term migration.

WHY Migrate?

The migrate module provides a flexible framework for migrating content into Drupal from other sources (e.g., when converting a website from another CMS to Drupal). Out-of-the-box, support for creating core Drupal objects such as nodes, users, files, terms, and comments are also included - it can easily be extended for migrating other types of content. Content is imported and rolled back using a bundled web interface (Migrate UI module) or included Drush commands.

In one of the recent projects, we needed to migrate terms,nodes and author from external sources. For this scenario  we are implementing our own migration script.

HOW to migrate?

For implementing migration script, there are some dependencies for which you need to install all the required modules which are listed below.

Migrate Module (Included in Drupal 8 core)

The migrate module provides a flexible framework for migrating content into Drupal from other sources.

Related: How to migrate your website to Drupal 8

Drupal Console
The new CLI for Drupal. Drupal Console is a tool to generate boilerplate code, interact and debug Drupal. From the ground up, it has been built to utilize the same modern PHP practices which were introduced in Drupal 8.

Migrate Plus
The migrate_plus project contains three modules:

  • migrate_plus - Extends the core migration framework with additionalfunctionalities to tweak incoming source data in migrations, also to code examples for making custom migrations
  • migrate_example - A carefully documented implementation of a custom migration scenario, designed to walk you through the basic concepts of the Drupal 8 migration framework.
  • migrate_example_advanced (still in progress) - Examples of more advanced techniques for Drupal 8 migration. 

Migrate Tools

Drush commands supported include:

  • migrate-status - Lists migrations and their status.
  • migrate-import - Performs import operations.
  • migrate-rollback - Performs rollback operations.
  • migrate-stop - Cleanly stops a running operation.
  • migrate-reset-status - Sets a migration status to Idle if it gets stuck.
  • migrate-messages - Lists any messages associated with a migration import.

To use Drupal with Drush in Drupal 8 it requires a minimum of Drush version 8. Before you get your hands dirty, you can go thru' this article on Custom Drush commands in Drupal 8. This should give you a good understanding about writing custom Drush commands in Drupal 8. It is not exactly like Drupal 7 but similar.

First, connect Drupal with an external database and add this code in settings.php
We are going to instruct migrate source to use this database target and use the key name as “source_migration” which will be  used in every migration file.

Here we go, with our Custom script for creating Taxonomy Term From External Source.

Which will eventually help to organize your site by assigning descriptive terms to each piece of content

Open terminal and enter 

drupal migrate:debug

As our migration resources are not showing yet we have to create a custom module for this. We can do this by the following steps -

  1. Create folder for custom Drupal module in my Drupal site: /modules/custom/migration_fj
  2. Create a new file migration_fj.info.yml with below code
  3. Migration plus module allows you to specify even the migration group. For grouping our migration we need to create a config entity. Create a new file migration_fj.migration_group.fj.yml using code below in the module folder custom/migration_fj/config/install/

    Here we have used our Key “source_migration” for connecting migration script with external database from where we would be fetching the content.

  4. Now let’s move on to defining source class.

    In our project, we needed to import category from a custom application as taxonomy terms in Drupal. In this case, the category didn’t have unique ids, instead, it was just a column having table name as a category with the values in rows.

    To create source class, create a new file.

  5. Now for term migration, we need to create a new config entity.

Our module is complete and ready for term migration using Drush. Now we can execute it with the migrate_tools

drush migrate-import example_term

Related: Migrating address book using process plugin in Drupal 8

WHAT if you have to roll it back?

For rollback, we can use drush migrate-rollback

I hope now it will be easier to migrate to Drupal 8.

Comment below if you come across any difficulties during the process. Read more of my experiences with Drupal.

Read More Relevant: How to migrate Users from a CSV file in Drupal 8?