You are here

public function Username::applyAccountFormProtection in User protect 8

Implements applyAccountFormProtection::isEnabled().

By default, no protection is applied.

Overrides UserProtectionBase::applyAccountFormProtection

File

src/Plugin/UserProtection/Username.php, line 21

Class

Username
Protects user's name.

Namespace

Drupal\userprotect\Plugin\UserProtection

Code

public function applyAccountFormProtection(array &$form, FormStateInterface $form_state) {
  $build_info = $form_state
    ->getBuildInfo();
  $account = $build_info['callback_object']
    ->getEntity();

  // If for some reason the account has no username, then don't protect it.
  if ($account
    ->getAccountName() && isset($form['account']['name'])) {
    $form['account']['name']['#disabled'] = TRUE;
    $form['account']['name']['#value'] = $account
      ->getAccountName();
    return TRUE;
  }
  return FALSE;
}