0

Using the Migrate module, I created an import task that creates and updates user info from a csv file. The update is launched using drush.

drush -l http://localhost.atrium.com mi Utilisateur --update

All the users are updated appropriately but the user roles, which are not in the migration mapping, are reset for every user that's updated.

How can I avoid that and keep the user roles for every account that's updated ?

Thanks !

3 Answers 3

0

You can add fields to be explicitly ignored by adding addUnmigratedDestinations to your class:

$this->addUnmigratedDestinations(array(
  'roles',
  'role_names',
));
1
  • For roles, they were automatically reset in the user.inc file of the Migration module ... we patched it and now it's fine. Now we realized we have the same problem with the organic groups group membership entityreference. It's automatically reset after the migration. Even if I explicitly ignore it like in your snippet, it's still reset.
    – agodin
    Feb 5, 2016 at 19:47
0

I experienced the same problem today. I import users from a csv file. Roles have been set manually on the imported users and were removed on second migrate import with --update flag.

I searched for a solution that does not need to hack the migration files and my solution, that works for me, is the following.

I set "roles" to map my Source CSV "roles" column, which is not in the csv file itself and add that field in the prepareRow function of the migration.

public function prepareRow($current_row) {
// Always start your prepareRow implementation with this clause. You need to
// be sure your parent classes have their chance at the row, and that if
// they return FALSE (indicating the row should be skipped) you pass that
// on.
if (parent::prepareRow($current_row) === FALSE) {
  return FALSE;
}

// We need to preserve the users roles, if this is a user, that has been
// imported before.
$current_row->roles = array();
if (!empty($current_row->migrate_map_destid1)) {
  $account = user_load($current_row->migrate_map_destid1, TRUE);
  if (!empty($account->roles)) {
    $current_row->roles = array_keys($account->roles);
  }
}

return TRUE;

}

-1

We added these lines after line 162 in the user.inc file to not reset the user roles ...

    //and keep the roles too
    $old_account = user_load($account->uid, TRUE);
    $account->role_names = $old_account->roles;
1
  • You should not change the core's files.
    – Haza
    Jul 12, 2018 at 14:02

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.