You are here

private static function UserCsvImportController::createUser in User CSV import 2.0.x

Same name and namespace in other branches
  1. 8 src/Controller/UserCsvImportController.php \Drupal\user_csv_import\Controller\UserCsvImportController::createUser()
  2. 1.0.x src/Controller/UserCsvImportController.php \Drupal\user_csv_import\Controller\UserCsvImportController::createUser()

Creates a new user from prepared values.

Parameters

array $values: Values prepared from prepareRow().

Return value

\Drupal\user\Entity\User

1 call to UserCsvImportController::createUser()
UserCsvImportController::processUpload in src/Controller/UserCsvImportController.php
Processes an uploaded CSV file, creating a new user for each row of values.

File

src/Controller/UserCsvImportController.php, line 181

Class

UserCsvImportController
Provides methods to import CSV files and convert to users.

Namespace

Drupal\user_csv_import\Controller

Code

private static function createUser($values) {
  if (user_load_by_mail($values['mail']) === FALSE) {
    $user = User::create($values);
    try {

      // If new user stores well, return the user object.
      if ($user
        ->save()) {
        return $user;
      }
    } catch (EntityStorageException $e) {
      \Drupal::messenger()
        ->addMessage(t('Could not create user (username: %uname) (email: %email); exception: %e', [
        '%e' => $e
          ->getMessage(),
        '%uname' => $values['name'],
        '%email' => $values['mail'],
      ]), 'error');
    }
  }
  else {
    $message = t('Could not create user (username: %uname) (email: %email). Email already in use', [
      '%uname' => $values['name'],
      '%email' => $values['mail'],
    ]);

    // put in logs
    \Drupal::logger('usercsvimport')
      ->notice($message);

    // output to user submitting
    \Drupal::messenger()
      ->addError($message);
  }
  return FALSE;
}