0

I have a migration with an image field and the files already exist on the server where I want them.

There is a file, some_file.jpg that already exists at sites/default/files/some_file.jpg but when I run the migration, it tries to copy the file from sites/default/files/some_file.jpg to sites/default/files/node_type/some_file.jpg and then also creates another file entity for it. I just want it to re-use the file and file entity that already exist for it. The destination field type is an image field.

I have this on the field mapping for the file field:

$this->addFieldMapping('field_upload_image', 'field_res_upload_photo');
$this->addFieldMapping('field_upload_image:preserve_files')
     ->defaultValue(TRUE);
$this->addFieldMapping('field_upload_image:file_replace')
     ->defaultValue(MigrateFile::FILE_EXISTS_REUSE);
0

Well if thats not working do a scan for the file on the destination location to set or unset the field value (so it's not updated on save). You can do this in the prepare function of your migration class. See the commonly implemented migration methods page for example implementations.

Also have you tried setting the preserved_files key:

    $this->addFieldMapping('field_images:preserve_files')
  ->defaultValue(TRUE);

I haven't done any file migrations myself lately, but I stumbled across that in this thread: https://www.drupal.org/node/1886404 it looks like you got the field ordering correct (well from the issue the source field primary and sub-field ordering now no longer matters).

  • That's in the code above, but I am now trying to change to this: $this->addFieldMapping('field_upload_image:file_class') ->defaultValue('MigrateFileFid'); I'm hoping that gives me better results as it is easier to get FID and add to to the row. – CR47 Sep 4 '14 at 15:26
-1

I normally use file_copy function to update this manually. Set target folder to be same as copy from folder.

  1. Use drupal_realpath function to transfer directory path to image into drupal file path. e.g /sites/default/node_type/some_file.jpg into public://node_type/some_file.jpg

  2. set file object

  3. use file_copy($file, 'public://node_type',FILE_EXISTS_REPLACE); this will not copy file if file is exists.

  4. set this file into your node. and update it. something like this

    $node->field_category_image = array(LANGUAGE_NONE => array( (array) $file)); node_save($node);

whole code

$filepath = drupal_realpath('/sites/default/node_type/some_file.jpg');
      $file = (object) array(
        'uid' => 1,
        'uri' => $filepath,
        'filemime' => file_get_mimetype($filepath),
        'status' => 1,
      );

$file = file_copy($file, 'public://node_type',FILE_EXISTS_REPLACE);
$node->field_image = array(LANGUAGE_NONE => array( (array) $file));
node_save($node);
  • This question deals with the migrate module and how it handles file fields' data during migration not simply saving files, so this would not help. – CR47 Sep 5 '14 at 17:26
  • I know it is not relate to migrate module, but you could migrate other field first, and manually save the image field you want after migrate module runs. it will still solve your duplicate file problem. – louieliu Sep 6 '14 at 20:32
  • There are issues with rolling back and other migration related issues in this case. – CR47 Sep 12 '14 at 15:15

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.