Hello,

I have several file and image fields attached to users on the source D7 website that I'm trying to migrate.
I extended DrupalUser7Migration and added those field mappings:

// File fields.
$this->addFieldMapping('field_user_profile_picture', 'field_profile_picture')
  ->sourceMigration('CrosMigrateImportFiles');
$this->addFieldMapping('field_user_profile_picture:file_class')
  ->defaultValue('MigrateFileFid');
$this->addFieldMapping('field_user_profile_picture:width', 'field_profile_picture:width');
$this->addFieldMapping('field_user_profile_picture:height', 'field_profile_picture:height');

$this->addFieldMapping('field_user_cv', 'field_cv')
  ->sourceMigration('CrosMigrateImportFiles');
$this->addFieldMapping('field_user_cv:file_class')
  ->defaultValue('MigrateFileFid');
$this->addFieldMapping('field_user_cv:display', 'field_cv:display');

All files are imported correctly in the file_managed table by my DrupalFile7Migration (called CrosMigrateImportFiles) but the tables related to those 2 field mappings above are not populated.

I guess I should do something similar to the picture migration which I tried but with no luck.
Any idea how to achieve this?

Thank you very much,
Hervé

Comments

herved created an issue. See original summary.

herved’s picture

Status: Active » Fixed

Ok I managed to do this by importing those files first (using a dedicated migration below).
Then I had to reference it on the field mappings in my user migration - using ->sourceMigration(MyMigrateImportUserFiles).
And finally update the uid in the file_managed table in the complete method of my user migration.

class MyMigrateImportUserFiles extends DrupalFile7Migration {
  public function __construct(array $arguments) {
    $arguments['file_class'] = 'MigrateFileFid';
    parent::__construct($arguments);
  }

  protected function query() {
    // Profile picture files.
    $pic_query = Database::getConnection('default', $this->sourceConnection)
      ->select('file_managed', 'f')
      ->fields('f');
    $pic_query->innerJoin('field_data_field_profile_picture', 'fpp', 'f.fid = fpp.field_profile_picture_fid');
    $pic_query->innerJoin('users', 'u', 'fpp.entity_id = u.uid');

    // CV files.
    $cv_query = Database::getConnection('default', $this->sourceConnection)
      ->select('file_managed', 'f')
      ->fields('f');
    $cv_query->innerJoin('field_data_field_cv', 'fc', 'f.fid = fc.field_cv_fid');
    $cv_query->innerJoin('users', 'u', 'fc.entity_id = u.uid');

    // Union those 2 queries to get all user files.
    $query = Database::getConnection('default', $this->sourceConnection)
      ->select($pic_query->union($cv_query), 't')
      ->fields('t')
      ->orderBy('t.timestamp');

    return $query;
  }
}

Switching this to fixed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.