You are here

function commerce_order_unique_username in Commerce Core 7

Takes a base username and returns a unique version of the username.

Parameters

$base: The base from which to construct a unique username.

Return value

A unique version of the username using appended numbers to avoid duplicates if the base is already in use.

1 call to commerce_order_unique_username()
commerce_order_get_properties in modules/order/commerce_order.module
Callback for getting order properties.

File

modules/order/commerce_order.module, line 1425
Defines the core Commerce order entity and API functions to manage orders and interact with them.

Code

function commerce_order_unique_username($base) {
  $username = $base;
  $i = 1;
  while (db_query('SELECT 1 FROM {users} WHERE name = :name', array(
    ':name' => $username,
  ))
    ->fetchField()) {

    // Ensure the username does not exceed the maximum character limit.
    if (strlen($base . $i) > USERNAME_MAX_LENGTH) {
      $base = substr($base, 0, strlen($base) - strlen($i));
    }
    $username = $base . $i++;
  }
  return $username;
}