2

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?

3 Answers 3

3

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.

0

This is with ref to Drupal 8/9

Create your CSV with Roles column containing : machine_name only seperated by only comma and no spaces around ex; role_update,role_create

Update CSV data accordingly, this is an example for reference

First Name Last Name Name      E-Mail     Roles
fname      lname     username  user@email role_update,role_create # No Space in the Cell of Roles data

Keep your CSV at public folder : webroot/sites/default/files/User-List-D7.csv

YML file should include the import in following way:

id: user_migration_csv
migration_tags: null
migration_group: Usermigration
label: 'User migration from CSV'
source:
  plugin: csv
  path: public://User-List-D7.csv
  header_row_count: 1
  ids:
    - ID   
process:
  field_first_name: First Name
  field_last_name: Last Name
  name: Name
  mail: E-mail
  roles:
    plugin: explode
    delimiter: ','
    source: Roles

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.