3

I am using migrate_d2d module to migrate my Drupal 6 website to Drupal 7, I succeed to transfer every thing except for images.

I read the new documentation sever times and I managed to migrate the files into the file_managed but I cant link them with the nodes any hints or examples will be greatly appreciated

2 Answers 2

4

In your migrate register function define the files table migration.

/**
 * Migrate files table
 */
// The description and the migration machine name are also required arguments,
// which will be unique for each migration you register.
$file_arguments = $common_arguments + array(
  'description' => t('Migration of files from Drupal 6'),
  'machine_name' => 'Files',
  'source_dir' => '/var/www/drupal6/files/xxxx/',
);

// We just use the migrate_d2d D6 migration class as-is.
Migration::registerMigration('DrupalFile6Migration', $file_arguments['machine_name'],
$file_arguments);

Then in your node migration class set the files migration as the source.

// Map the image field to the new image field, base this on the Files migration
// to map the files to the imported files table.
$this->addFieldMapping('field_image', 'field_image')
  ->sourceMigration('Files');
$this->addFieldMapping('field_image:file_class')
  ->defaultValue('MigrateFileFid');

Hope that helps

2
  • thanks ,I think this will do very well I was missing $this->addFieldMapping('field_image:file_class') ->defaultValue('MigrateFileFid'); will try it and come back to you
    – Sari
    Commented Nov 8, 2012 at 10:13
  • Does this work also for user profile pics?
    – Uddhi
    Commented Jan 2, 2014 at 21:17
3

Migrating images and files are similar. Check this: Migrate Contents from Drupal 6 to Drupal 7 Using Migrate 2

Here's the pieces of code for images and files migration. You can put it in your migration class and remember to change the database name, field names, urls to yours.

<?php
// Add other source fields which may be populated in prepareRow()
$source_fields = array(
  'image_filename' => t('img_filename'),
  'attachments_filename' => t('attachments_filename'),
  'attachments_filepath' => t('attachments_filepath'),
  'attachments_description' => t('attachments_description'),
);

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

$this->addFieldMapping('field_image', 'image_filename');
$this->addFieldMapping('field_image:source_dir')->defaultValue('http://DOMAINNAME.COM/sites/default/files/THE_DIR_YOU_PUT_IMAGES');

$this->addFieldMapping('field_attachments', 'field_attachments');
$this->addFieldMapping('field_attachments:source_dir', 'field_attachments_filepath');
$this->addFieldMapping('field_attachments:description', 'field_attachments_description');

public function prepareRow($current_row) {
  // Set image data for the image fields.
  $query = db_select(SOURCE_DATABASE . '.content_type_NAME', 'i')
  ->condition('i.vid', $current_row->vid, '=');
  $query->join(SOURCE_DATABASE . '.files', 'f', 'i.field_image_fid = f.fid');
  $query->addField('f', 'filename');
  $result = $query->execute();

  foreach ($result as $row) {
    $image = array(
      'filename' => $row->filename,
    );
    $current_row->image_filename = $image['filename'];
  }

  // Set file data for the attachment file fields.
  $query = db_select(SOURCE_DATABASE . '.content_field_attachment', 'a')
    ->fields('a', array('field_attachment_data'))
    ->condition('a.vid', $current_row->vid, '=');
  $query->join(SOURCE_DATABASE . '.files', 'f', 'a.field_attachment_fid = f.fid');
  $query->addField('f', 'filename');
  $query->addField('f', 'filepath');
  $query->orderBy('a.field_attachment_fid', 'ASC');
  $result = $query->execute();

  foreach ($result as $row) {
    $field_data = unserialize($row->field_attachment_data);
    $field = array(
      'filename' => $row->filename,
      'filepath' => str_replace($row->filename, '', $row->filepath),
      'description' => $field_data['description'],
    );
    $current_row->attachments_filename[] = $field['filename'];
    $current_row->attachments_filepath[] = 'http://DOMAINNAME.COM/' . $field['filepath'];
    $current_row->attachments_description[] = $field['description'];
  }
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.