1

I have migrated bunch of users using the Migrate Source CSV and am succeeded in it, But I want multi-roles to be defined for single user.

Here is my config file,

enter image description here

Here is my following cases that I have tried out in a CSV file and it failed,

enter image description here

Is there is anything am missing out?

2

First put your all roles inside double quote separating by comma (,) and be sure that your all roles on csv file must be role's machine name.

sample data of user.

then edit your config file something like this....

process: 
  name: name
  mail: email
  pass: password
  roles:
    plugin: explode
    delimiter: ','
    source: roles
  status: status
destination:
  plugin: 'entity:user'
  md5_passwords: true

you have to use plugin: explode provided by drupal to import multiple roles separated by comma (,).

And if you are importing password also then use md5_passwords: true

0

I had the same issue those days and I came up with the following workaround (I don't think is the best but at least works!)

You roles should be comma separated in the CSV. Also, I had to make the match between the rids as in D7 the rid where numbers (eg 1,2,4) and in D8 it was strings (eg admin, default_role, etc)

Create a custom module only for the migration and use hook_user_presave in your module file.

//check if the user has multiple roles comma separated
if( strpos($current_roles[1], ',') !== false )
{
  //split the roles
  $tmp = explode(', ', $current_roles[1]);
  foreach ($tmp as $role){
    //add each role separately to the user
    $user->addRole($role);
  }

  //remove the old roles added by the CSV (all user roles comma separated)
  $user->removeRole($current_roles[1]);
}
else{
  // Default role assigned to all other users - optional
  $user->addRole('default_role');
}

Also another way to do that is this but I didn't try it so I don't know if/how it works.

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.