Migrating content from different sources to Drupal

June 18, 2021

Tags: Technologies, Tech Trends, Managed Teams
Share

Table of contents

Quick Access

drupal

 

Migrating data into a Drupal 7 site from an external source can be time-consuming, especially if you're unfamiliar with migration processes and data sources. Fortunately, the Migrate module simplifies this task by providing a powerful and easy-to-use interface.

 

This guide walks you through an example of migrating data from a database table to create nodes of a content type in Drupal. For this demonstration, we will use a content type called Activities, which includes a title, body, and image field.

 

Setting Up the Migrate Module

  1. Enable the Necessary Modules
    Start by enabling the Migrate module and its user interface extension, Migrate UI, using Drush:

    drush en migrate -y drush en migrate_ui -y 
  2. Create a Custom Migration Module
    Create a custom module named migrate_activities. In the module’s .info file, declare the Migrate module as a dependency:

    name = Migrate Activities description = Custom migration module for activities. dependencies[] = migrate core = 7.x files[] = activities.inc 
  3. Add Necessary Files
    Your module will consist of four files:
    • migrate_activities.info
    • migrate_activities.module (can remain empty)
    • migrate_activities.migrate.inc
    • activities.inc (contains the migration class definition)

 

Defining the Migration Group

Use the hook_migrate_api() in the migrate_activities.migrate.inc file to register the migration group:

 

/** * Implements hook_migrate_api(). */ function migrate_activities_migrate_api() {    $api = array(        'api' => 2,        'groups' => array(            'activities_group' => array(                'title' => t('Activity Migrations'),            ),        ),        'migrations' => array(            'activities' => array(                'class_name' => 'MigrateActivities',                'group_name' => 'activities_group',            ),        ),    );    return $api; } 

 

This hook registers the migration group (activities_group) and the associated class (MigrateActivities).

 

Defining the Migration Class

Create the activities.inc file and define the MigrateActivities class. Ensure the class extends the Migration class:

 
/** * Class MigrateActivities */ class MigrateActivities extends Migration {    public function __construct($arguments) {        parent::__construct($arguments);        // Define the source query        $query = Database::getConnection('default', 'migrate_db')            ->select('data', 'a')            ->fields('a', array('id', 'title', 'description', 'imageUrl'));        // Map source fields        $fields = array(            'id' => 'Unique ID for each source data row',            'title' => 'The activity title',            'description' => 'The activity description',            'imageUrl' => 'An image URL of the activity',        );        $this->source = new MigrateSourceSQL($query, $fields);        // Define the destination        $this->destination = new MigrateDestinationNode('activities');        // Map fields        $this->map = new MigrateSQLMap($this->machineName, array(            'id' => array(                'type' => 'int',                'unsigned' => TRUE,                'not null' => TRUE,            ),        ), MigrateDestinationNode::getKeySchema());        $this->addFieldMapping('title', 'title');        $this->addFieldMapping('body', 'description');        $this->addFieldMapping('field_activity_image', 'imageUrl');    } } 

 

Configuring the Database Connection

Add a connection to the database from which data will be migrated in the settings.php file:

 
$databases['migrate_db']['default'] = array(    'database' => 'migrate',    'username' => 'root',    'password' => 'root',    'host' => 'localhost',    'driver' => 'mysql', ); 

 

Running the Migration

  1. Register the Migration
    Navigate to the migration configuration page:

    admin/content/migrate/configure 

    Click Register statically defined classes to register the migration.

     

  2. Start the Migration
    On the dashboard (admin/content/migrate), locate your migration group. Click the Import option to start the migration process.

     

  3. Verify the Results
    Once the migration is complete, new nodes will be created for the Activities content type. You can create a view to display the migrated content for verification.

 

The Migrate module in Drupal 7 is a robust tool for transferring data from various sources into your site. With its ability to handle complex migrations, this module is indispensable for projects involving significant content migration. By automating the process, you save hours of manual work and ensure consistency in the data migration.

 

For more details, refer to the official Migrate documentation on Drupal.org.

 

We recommend you this video

Let's work together!