Drupal - Migrate users and retain passwords

As @No Sssweat recommend, the D8 Migrate Modules is good way. With your way, you have to add property pre_hashed for field pass.

$values = [
  'name' => 'test',
  'mail' => '[email protected]',
  'roles' => [],
  'pass' => [
    'value' => $hashed_drupal_7_password,
    'pre_hashed' => TRUE,
  ],
  'status' => 1,
];
$account = entity_create('user', $values);
$account->save();

You can find this property in FieldType Password


You should use the D8 Migrate Modules to do this, specially for transferring 10K users, unless your current code is using the Batch API. Otherwise, you're going to run into timeouts because it's too big. Also, these migrate modules allow you to transfer from SQL to SQL. No need for CSV file, if you don't want. But you can still do CSV file import with them, if needed.


To answer your question programmatically, as far as I know, Drupal API doesn't offer a way to skip the hashing, probably for security reasons.

Since D7 and D8 use the same hashing, after User is created, you have to overwrite it by updating it manually/directly. Add this right after $account->save();.

// Updates user password
$uid = $account->id();
$query = \Drupal::database()->update('users_fields_data');
$query->fields([
  'pass' => $hashed_drupal_7_password,
]);
$query->condition('uid', $uid);
$query->execute();

Flush caches after.

Note: D6 uses different hashing, so this answer wouldn't work in that case.

Tags:

Migration

8