You are here

public static function UloginHelper::makeUsername in uLogin (advanced version) 8

Internal functions.

2 calls to UloginHelper::makeUsername()
UloginController::uloginCallback in src/Controller/UloginController.php
Call back for login and registration.
UserIdentityDelete::submitForm in src/Form/UserIdentityDelete.php
Form submission handler.

File

src/UloginHelper.php, line 167

Class

UloginHelper
Ulogin Helper class.

Namespace

Drupal\ulogin

Code

public static function makeUsername($data) {
  $pattern = \Drupal::config('ulogin.settings')
    ->get('username') ?: '[user:ulogin:network]_[user:ulogin:uid]';
  $ulogin_name = $desired_name = \Drupal::token()
    ->replace($pattern, [
    'user' => [
      'ulogin' => $data,
    ],
  ], [
    'clear' => TRUE,
    'sanitize' => FALSE,
  ]);
  $counter = 0;
  while (user_load_by_name($ulogin_name)) {
    $counter++;
    $ulogin_name = $desired_name . ' ' . $counter;
  }
  $name = $ulogin_name;
  \Drupal::moduleHandler()
    ->alter('ulogin_username', $name, $data);

  // Check that the altered username is unique.
  if ($name == $ulogin_name || user_load_by_name($name)) {
    return $ulogin_name;
  }
  else {
    return $name;
  }
}