1

I'm writing a file migration using migrate/migrate_d2d to migrate files from one Drupal 7 installation to another.

Images have custom fields attached to them, including a text field "field_boilerplate". This field exists in the destination site's image type. When I run the migration, the image itself migrates and creates an image entity, but field_boilerplate is not populated.

What am I doing wrong, and how can I migrate fields attached to files?

In mymodule.module:

/**
 * Implements hook_flush_caches().
 */
function mymodule_flush_caches() {
  mymodule_migrate_register_classes();
}
/**
 * Register dynamic migration classes
 */
function mymodule_migrate_register_classes() {
  // Arguments common to all migrations
  $common_args = array(
    'source_connection' => 'mysite_legacy',
    'source_version' => 7,
    'group_name' => 'mymodule_migration',
  );
  // Other migration arguments redacted for length
  // Image migration arguments
  $image_args = array(
    'machine_name' => 'images',
    'class_name' => 'MyModuleImageMigration',
    'description' => t('Migrate images'),
    'user_migration' => 'users',
    'default_uid' => 1,
    'source_dir' => 'http://example.com/sites/default/files',
    'destination_dir' => 'public://',
    'skip_pictures' => TRUE,
  );
  Migration::registerMigration('MyModuleImageMigration', 'images', $image_args + $common_args);
}
/**
 * Implements hook_migrate_api().
 */
function mymodule_migrate_api() {
  return array('api' => 2);
}

In an included file:

<?php
class MyModuleImageMigration extends DrupalFile7Migration {
  public function __construct($arguments) {
    parent::__construct($arguments);
    // Mappings
    $this->addFieldMapping('field_boilerplate', 'field_boilerplate');
  }
}
2

You need to specify the bundle type when registering the dynamic migration:

  $image_args = array(
    'machine_name' => 'images',
    'class_name' => 'MyModuleImageMigration',
    'description' => t('Migrate images'),
    'user_migration' => 'users',
    'default_uid' => 1,
    'source_dir' => 'http://example.com/sites/default/files',
    'destination_dir' => 'public://',
    'skip_pictures' => TRUE,
    'bundle' => 'image', // Need to add the bundle type
  );
  Migration::registerMigration('MyModuleImageMigration', 'images', $image_args + $common_args);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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