I need to migrate from a WordPress site to a Drupal 7 site where users could upload their own images (like multiple user pictures). I have a CSV file with the User’s ID and a serialized array containing the filenames of uploaded pictures (no, really).
Users have a multi-value image field ‘field_user_pictures’.
After deserializing the array, drupal complains with the error “ltrim() expects parameter 1 to be string, array given”. (Presumably because MigrateDestinationFile doesn’t handle arrays very well.) Then I tried with just using the first image of the array, and that actually works. What doesn’t work, however, is to get drupal to link this sole image to the ‘field_user_pictures’ user field.
I have been trying to get to something like this:
class PictureMigration extends Migration { public function __construct ($arguments) { parent::__construct($arguments); $source = 'user_pictures.csv'; $columns = array( 1 => array('source_userid', ''), 3 => array('source_serialized', ''), ); $source_key = array('source_userid' => array('type' => 'int', 'not null' => TRUE),); $this->source = new MigrateSourceCSV($source, $columns); $this->destination = new MigrateDestinationFile('image', 'MigrateFileUri'); $this->map = new MigrateSQLMap($this->machineName, $source_key, MigrateDestinationFile::getKeySchema()); $this->addFieldMapping('source_dir')->defaultValue($this->remote_dir); $this->addFieldMapping('destination_dir')->defaultValue('public://'); $this->addFieldMapping('value', 'source_serialized'); } public function prepareRow($row) { // snipped $row->source_serialized = $this->deserializePhotos($row->source_serialized); // returns array of filenames } } public class UserMigration extends Migration { public class __construct ($arguments) { // snipped... $this->addFieldMapping('field_user_pictures')->sourceMigration('PictureMigration'); } }
The first problem is that MigrateDestinationFile doesn’t seem to like multi-value values; the second problem is that even when I only use the first filename of the array, it still doesn’t get linked to the ‘field_user_pictures’ field…
See also: Migration: Combine Profile2 and file migration — almost exactly the same issue, but with no valid answer as well.