Migration of multi-value pictures to user fields

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.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Migration of multi-value pictures to user fields

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.