Would be great to have native support for uploads added to nodes using the core D6 Upload module.

My current solution:

abstract class CustomNodeMigration extends DrupalNode6Migration {
  public function __construct(array $arguments) {
  
  	$this->sourceFields += array(
      'attachements_filename' => t('Attachments File Names from D6'),
      'attachements_filepath' => t('Attachments File Paths from D6'),
      'attachements_description' => t('Attachments Description'),
      'attachements_display' => t('Attachments Display Option'),
    );

    parent::__construct($arguments);

    $this->addFieldMapping('field_attachments', 'attachements_filepath');
    $this->addFieldMapping('field_attachments:file_class')->defaultValue('MigrateFileUri');
    $this->addFieldMapping('field_attachments:file_replace')->defaultValue('FILE_EXISTS_REUSE'); 
    $this->addFieldMapping('field_attachments:preserve_files')->defaultValue(FALSE);
    $this->addFieldMapping('field_attachments:source_dir')->defaultValue('files2migrate');
    $this->addFieldMapping('field_attachments:description', 'attachements_description');
    $this->addFieldMapping('field_attachments:display', 'attachements_display');
  }
  public function prepareRow($row) {
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    }    
    $row->attachements_fid = array();
    $row->attachements_filename = array();
    $row->attachements_filepath = array();
    $row->attachements_description = array();
    $row->attachements_display = array();

    $connection = Database::getConnection('default', $this->sourceConnection);
	$query = $connection->select('upload', 'up');
    $query->condition('up.vid', $row->vid);
    $query->fields('up', array('fid', 'description', 'list', 'weight'));
	$query->leftJoin('files', 'f', 'up.fid = f.fid');
	$query->fields('f', array('filename', 'filepath'));
    $results = $query->execute();
    foreach ($results as $result) {
      	$result->filepath = str_replace("sites/default/files/","",$result->filepath);
    	$row->attachements_fid[] = $result->fid;
    	$row->attachements_filename[] = $result->filename;
    	$row->attachements_filepath[] = $result->filepath;
    	$row->attachements_description[] = $result->description;
    	$row->attachements_display[] = $result->list;
    }
  }
}

Comments

Miro Goepel’s picture

Thumbs up!

This needs to be included.

alforddm’s picture

Will this work for comment upload as well?

alforddm’s picture

To answer my own question...Yes it does work for the comment upload module as well. I just had to change "field_attachments" to my comment attachment field, the database table from upload to comment_upload and changed vid to cid. It worked great! Thanks so much!

fredcy’s picture

This is great! I've been trying to figure out how to do exactly this. This would be a useful addition to migrate_d2d.

rooby’s picture

Version: 7.x-2.0-rc1 » 7.x-2.x-dev

This is definitely needed.

Is there a reason why attachments is often misspelled as attachements?

mikeryan’s picture

Assigned: Unassigned » mikeryan
Status: Active » Fixed

OK, I've added support for the upload module - if the upload table is present and the source node type has attachments enabled, source fields upload (corresponding to upload.fid), description, list, and weight are exposed as mappable fields.

Status: Fixed » Closed (fixed)

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

dasginganinja’s picture

Oh MikeRyan, I might have a case for you to look at!

Migration failed with source plugin exception: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'd6_goodshep_sand.upload' doesn't exist

This is using the latest dev versions of migrate, migrate_extras, migrate_d2d. Below I have included my class used and the definition I send to Migrate::register().

class SCNodeMigration extends DrupalNode6Migration {
  
  public function __construct(array $arguments) {
    // Let the parent do its work
    parent::__construct($arguments);
    
    $this->addUnmigratedSources(array(
      'uid',
      'revision',
      'log',
      'revision_uid',
    ), 'DNM');
    
    $this->addUnmigratedDestinations(array(
      'metatag_title',
      'metatag_description',
      'metatag_abstract',
      'metatag_keywords',
      'metatag_robots',
      'metatag_news_keywords',
      'metatag_generator',
      'metatag_copyright',
      //'metatag_img_src',
      'metatag_canonical',
      'metatag_shortlink',
      'metatag_publisher',
      'metatag_author',
      'metatag_original-source',
    ), 'DNM');
  }
  
  protected function query() {
    // Start with the parent query
    $query = parent::query();
    
    // Modify the query here
    
    // Return
    return $query;
  }
  
  public function prepareRow($row) {
    // Let the parent prepare the row before we get to it
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    }
    
    // Your logic here
  }
}

class SCPdfNodeMigration extends SCNodeMigration {
  
  public function __construct(array $arguments) {
    // Let the parent do its work
    parent::__construct($arguments);
    
    $this->addUnmigratedSources(array(
      'field_sweet_charity_pdf:list',
      'field_sweet_charity_thumb:list',
      'field_sweet_charity_pdf_data',
      'field_sweet_charity_thumb_data',
    ), 'DNM');
    $this->addFieldMapping('field_sweet_charity_pdf','field_sweet_charity_pdf')
      ->sourceMigration('SCPdfFile');
    $this->addFieldMapping('field_sweet_charity_pdf:file_class')
     ->defaultValue('MigrateFileFid');
    $this->addFieldMapping('field_sweet_charity_thumb','field_sweet_charity_thumb')
      ->sourceMigration('SCPdfFile');
    $this->addFieldMapping('field_sweet_charity_thumb:file_class')
     ->defaultValue('MigrateFileFid');
    //$this->addFieldMapping('field_sweet_charity_pdf','pdfs')->separator(',');
    //$this->addFieldMapping('field_sweet_charity_pdf:source_dir')->defaultValue('http://www.goodshepherdrehab.org/sites/goodshepherdrehab.org/files/');
  }
  
  protected function query() {
    // Start with the parent query
    $query = parent::query();
    //$query->leftJoin('field_data_field_sweet_charity_pdf','pdf','n.nid = pdf.entity_id');
    //$query->leftJoin('file_managed', 'file', 'file.fid = pdf.field_sweet_charity_pdf_fid');
    //$query->addExpression('GROUP_CONCAT(pdf.filename)','pdfs');
    // Modify the query here
    
    // Return
    return $query;
  }
  
  public function prepareRow($row) {
    // Let the parent prepare the row before we get to it
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    }
    if (function_exists('drush_print')) {
      //drush_print_r($row);
    }
    // Your logic here
  }
}
// This stores the information about migrations (arguments, names, etc.)
  $arr_migrations = array();
  
  // Define our common arguments
  $common_arguments = array(
    'source_version' => 6,
    'group_name' => 'Sweet Charity Migration',
    'source_connection' => 'legacy',
    'source_database' => array(
      'driver' => 'mysql',
      'database' => 'd6_sand',
      'username' => 'd7_sand',
      'password' => 'xxx',
      'host' => '127.0.0.1',
      'prefix' => '',
    ),
  );
  
  // General Node Arguments
  $node_arguments = $common_arguments + array(
    'default_uid' => 1,
  );

  // PDF Node Arguments
  $pdf_node_arguments = $node_arguments + array(
    'machine_name' => 'SCPdfNode',
    'description' => t('Import Drupal 6 PDF Nodes'),
    'source_type' => 'sweet_charity_pdf',
    'destination_type' => 'sweet_charity_pdf',
  );
  // Add the PDF Node to the list of migrations
  $arr_migrations['SCPdfNodeMigration'] = $pdf_node_arguments;

  // Loop through all migrations and register them
  foreach($arr_migrations as $k => $v) {
    Migration::registerMigration($k, $v['machine_name'], $v);
  }

Thanks in advance!