Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Stuck with migration of user pictures.
I have Drupal 7 with Migration 7.x-2.2 on board.
Source DB have user_avatar field with plain filename in it with user's avatar.
Destination (user.inc) gets picture as file object. But MigrateFileFieldHandler doesn't handle user->picture fields as this is not actual file field.
What is the best way to migrate those without heavy copy-pasting file_* routines.
Thanks.
Comments
Comment #1
TahitiPetey CreditAttribution: TahitiPetey commentedsubscribe
Comment #2
Andrey Zakharov CreditAttribution: Andrey Zakharov commentedI figured this out.
variable_get('user_picture_path', 'pictures')
)AvatarsMigration extends Migration
with source of just field user_avatar from user table, and destination MigrateDestinationFile (see below)UsersMigration extends Migration
as usual entity migration withComment #3
Andrey Zakharov CreditAttribution: Andrey Zakharov commentedComment #4
elBradford CreditAttribution: elBradford commentedThis was helpful - it seemed to me that such a common migration as Drupal 6 to Drupal 7 would have an easier solution with regards to user avatars. Thanks for the post.
Comment #5
empowerfull CreditAttribution: empowerfull as a volunteer commentedThere's a much easier way. For those of us pulling from a non-Drupal site user by user with image variable $avatar:
// First define and upload the file. It will automatically return a fid in the file object and upload the file correctly.
$file = new StdClass();
$file->uid = 1;
$file->uri = "../" . $avatar;
$file->filemime = file_get_mimetype($file->uri);
$file->status = 1;
$file->display = 1;
$file->description = "";
$dest = file_default_scheme() . '://pictures';
$file = file_copy($file, $dest);
// Then set up the fields you wish to migrate. To tie the image file to the user fields, simply pass in the $file->fid
//set up the user fields
$fields = array(
'name' => $username,
'mail' => $email,
'pass' => $password,
'status' => 1,
'timezone' => $tz,
'init' => $email,
'roles' => array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
),
'picture'=> $file->fid
);
//the first parameter is left blank so a new user is created
$account = user_save('', $fields);