You are here

function _pmperson_migrate_generate_unique_username in Drupal PM (Project Management) 8

Same name and namespace in other branches
  1. 7.3 pmperson/includes/pmperson.migrate.inc \_pmperson_migrate_generate_unique_username()
  2. 7.2 pmperson/includes/pmperson.migrate.inc \_pmperson_migrate_generate_unique_username()

A custom function to generate unique username.

1 call to _pmperson_migrate_generate_unique_username()
_pmperson_migrate_create_user in pmperson/includes/pmperson.migrate.inc
Create a Drupal User.

File

pmperson/includes/pmperson.migrate.inc, line 472
Migration functions for the PM Person module.

Code

function _pmperson_migrate_generate_unique_username($username, $reset = FALSE, $tries = array()) {
  static $suffix = 1;
  if ($reset) {
    $suffix = 1;
  }
  if ($suffix < 2) {
    $duplicate = _pmperson_migrate_check_if_username_exists($username);
    if ($duplicate == FALSE) {
      return $username;
    }
  }
  else {
    $duplicate = _pmperson_migrate_check_if_username_exists("{$username} {$suffix}");
  }
  if (!empty($duplicate) and !empty($tries)) {
    foreach ($tries as $try) {
      if (_pmperson_migrate_check_if_username_exists($try) == FALSE) {
        return $try;
      }
    }
  }

  // Loop until username is valid.
  if (!empty($duplicate)) {
    $suffix++;
    _pmperson_migrate_generate_unique_username($username);
  }

  // Add number at end of username if it already exists.
  $username = $suffix < 2 ? $username : "{$username} {$suffix}";
  return $username;
}