HomeDrupalDrupal Migrate: Importing Images

Drupal Migrate: Importing Images

I recently spent quite a bit of time with our friend the Migrate module. Along the way I picked up a few nuggets of knowledge that may save somebody out there a few hours of time trying to piece together.

Problem:

This problem arose from a Access to Drupal migration where zero or many images were associated with a piece of content. The Access database didn’t know anything about the images, it was only the naming convention of the images which associated them with a piece of content (e.g., [PRIMARY KEY].jpg).

Here are the steps I used to pull these images into drupal during the migrate process.

1. Set up your entity (node or otherwise) to contain an image field.

2. Construct your migrate as you normally would. In my case I used a query to pull the data from recently imported mysql tables (Access Data) as the source data.


public function __construct() {
  $query = db_select('access_table', 'access');
  $query->fields('access', array(‘PK_column’, …));
  $query->orderBy('PK_column', 'ASC');
  
  // Create a MigrateSource object, which manages retrieving the input data.
  $this->source = new MigrateSourceSQL($query);
  
  $this->destination = …;

3. Use the prepareRow function call to find and prepare the images for import.

In this function I used php glob to search for an image in a specific directory. (I had earlier copied all of the images to the sites/default/files/[sub folder] folder


public function prepareRow($current_row) {
  $image_dir = variable_get('file_public_path', conf_path() . '/files') . '/[sub folder]';
  $webimages = glob($image_dir .'/'.$current_row->PK_column.'.jpg');
  $current_row->web_image_source = array();
  $current_row->web_image_source_dir = array();
  $current_row->web_image_destination_dir = array();
  $current_row->web_image_title = array();
  $current_row->web_image_alt = array();
  $current_row->web_image_preserve_files = array();
  $current_row->web_image_file_replace = array();

  foreach ($webimages AS $image) {
    $current_row->web_image_source[] = basename($image);
    $current_row->web_image_source_dir[] = $image_dir;
    $current_row->web_image_title[] = '';
    $current_row->web_image_alt[] = '';
    $current_row->web_image_preserve_files[] = TRUE;
    $current_row->web_image_file_replace[] = 'FILE_EXISTS_REUSE';
  }
}

4. Back in the construct function, add the field mappings.


public function __construct() {

…

// Web image migrate
   $this->addFieldMapping('field_product_image', 'web_image_source');
   $this->addFieldMapping('field_product_image:source_dir', 'web_image_source_dir');
   $this->addFieldMapping('field_product_image:alt', 'web_image_alt');
   $this->addFieldMapping('field_product_image:title', 'web_image_title');
   $this->addFieldMapping('field_product_image:preserve_files', 'web_image_preserve_files');
   $this->addFieldMapping('field_product_image:file_replace', 'web_image_file_replace');

All together:


public function __construct() {
  $query = db_select('access_table', 'access');
  $query->fields('access', array(‘PK_column’, …));
  $query->orderBy('PK_column', 'ASC');

  // Create a MigrateSource object, which manages retrieving the input data.
  $this->source = new MigrateSourceSQL($query);
  $this->destination = …;

  // Web image migrate
  $this->addFieldMapping('field_product_image', 'web_image_source');
  $this->addFieldMapping('field_product_image:source_dir', 'web_image_source_dir');
  $this->addFieldMapping('field_product_image:alt', 'web_image_alt');
  $this->addFieldMapping('field_product_image:title', 'web_image_title');
  $this->addFieldMapping('field_product_image:preserve_files', 'web_image_preserve_files');
  $this->addFieldMapping('field_product_image:file_replace', 'web_image_file_replace');
}

public function prepareRow($current_row) {
  $image_dir = variable_get('file_public_path', conf_path() . '/files') . '/[sub folder]';
  $webimages = glob($image_dir .'/'.$current_row->PK_column.'.jpg');
  $current_row->web_image_source = array();
  $current_row->web_image_source_dir = array();
  $current_row->web_image_destination_dir = array();
  $current_row->web_image_title = array();
  $current_row->web_image_alt = array();
  $current_row->web_image_preserve_files = array();
  $current_row->web_image_file_replace = array();

  foreach ($webimages AS $image) {
    $current_row->web_image_source[] = basename($image);
    $current_row->web_image_source_dir[] = $image_dir;
    $current_row->web_image_title[] = '';
    $current_row->web_image_alt[] = '';
    $current_row->web_image_preserve_files[] = TRUE;
    $current_row->web_image_file_replace[] = 'FILE_EXISTS_REUSE';
  }
}