Allows altering a user during importing

Created on 11 August 2023, over 1 year ago
Updated 22 May 2024, 6 months ago

Problem/Motivation

When the users importing, can be cases if we want make more actions. For example update more complex fields like paragraph or node reference.

Proposed resolution

Create hook_user_csv_import_create_user_alter().

API changes

Create file with Hooks and API provided by the "User CSV Import" module.

A real case of using "Allow user to log in via CAS β†’ module".

/**
 * Implements hook_user_csv_import_create_user_alter().
 */
function custom_module_user_csv_import_create_user_alter(UserInterface &$user) {
  // Allow user to log in via CAS.
  $casUserManager = \Drupal::service('cas.user_manager');
  $casUserManager->setCasUsernameForAccount($user, $user->getAccountName());
  // Generate random password.
  $random = new Random();
  $user->setPassword($random->string());
  if ($user->hasField('field_department')
    && $values['field_department'] !== '') {
    $tid = custom_module_create_terms('person_department', [$values['field_department']]);
    $user->set('field_department', $tid);
  }
  $user->save();
}
✨ Feature request
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024