0

I am currently migrating a D6 site into D7 and so far everything has gone smoothly. I use migrate and migrate_d2d.

But now I am stuck :-(. I have an image field with unlimited values (a slideshow) and into that field I need to import images from the corresponding slideshow in the legacy site. My problem is that after import only one image is imported (the last one in the slideshow). How do I make every picture end up in its own subfield? In my prepareRow() I am trying to use the loop below. I fetch the field ID from $row->fields_productimages[#] (index > 2 are field ID's that belong to the slideshow). I then get the file path from the legacy database and use that path to import the image. But as I said, only the last image is attached to my field. There are no error messages. Any help is appreciated.

$i = 2;
if($row->field_productimages[$i] != ''){
    while($row->field_productimages[$i] != ''){
        $fid = $row->field_productimages[$i];
        $filepath = Database::getConnection($target='default', $key='legacy')->query('SELECT f.filepath FROM {files} f WHERE f.fid = :fid',array(':fid' => $fid))->fetchAssoc();
        $this->addFieldMapping('field_bildspel_instrument')->defaultValue($filepath);   
        $this->addFieldMapping('source_dir')->defaultValue('http://www.mysite.se/');
        $this->addFieldMapping('destination_dir')->defaultValue('public://instrument/bildspel');    
        $this->addFieldMapping('field_bildspel_instrument:file_replace')->defaultValue('FILE_EXISTS_REPLACE');
        $this->addFieldMapping('field_bildspel_instrument:file_class')->defaultValue('MigrateFileUri');
        $this->addFieldMapping('field_bildspel_instrument:title', 'field_sensorimage:title');   
        $this->addFieldMapping('field_bildspel_instrument:preserve_files')->defaultValue(1);    
        $this->addFieldMapping('field_bildspel_instrument:urlencode')->defaultValue(1); 
        $i++;
    }
}
0

Finally figured it out. The image field with unlimited values needs a default value with file paths presented as an array. As the array in $filepath was 2 dimensional (an array in an array), I needed two foreach loops to get down to the simple array needed. This is now a working example, I publish it here in case somebody have the same question:

$i = 2;
$filepath = array();
if($row->field_productimages[$i] != ''){
    while($row->field_productimages[$i] != ''){
        $fid = $row->field_productimages[$i];
        $filepath[] = Database::getConnection($target='default', $key='legacy')->query('SELECT f.filepath FROM {files} f WHERE f.fid = :fid',
        array(':fid' => $fid))->fetchAssoc();
        $i++;
    }
    foreach ($filepath as $value1){
        foreach ($value1 as $value){
          $files[] = $value;
        }
    }
    $this->addFieldMapping('field_bildspel_instrument')->defaultValue($files);  
    $this->addFieldMapping('field_bildspel_instrument:source_dir')->defaultValue('http://www.myoldsite.se/');
    $this->addFieldMapping('destination_dir')->defaultValue('public://product/slideshow');  
    $this->addFieldMapping('field_bildspel_instrument:file_replace')->defaultValue('FILE_EXISTS_REUSE');
    $this->addFieldMapping('field_bildspel_instrument:file_class')->defaultValue('MigrateFileUri');
    $this->addFieldMapping('field_bildspel_instrument:title', 'field_productimages:title'); 
    $this->addFieldMapping('field_bildspel_instrument:preserve_files')->defaultValue(1);    
    $this->addFieldMapping('field_bildspel_instrument:urlencode')->defaultValue(1);     
}

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.