function RegisterForm::form

Same name and namespace in other branches
  1. 9 core/modules/user/src/RegisterForm.php \Drupal\user\RegisterForm::form()
  2. 8.9.x core/modules/user/src/RegisterForm.php \Drupal\user\RegisterForm::form()
  3. 10 core/modules/user/src/RegisterForm.php \Drupal\user\RegisterForm::form()

Overrides AccountForm::form

File

core/modules/user/src/RegisterForm.php, line 17

Class

RegisterForm
Form handler for the user register forms.

Namespace

Drupal\user

Code

public function form(array $form, FormStateInterface $form_state) {
    
    /** @var \Drupal\user\UserInterface $account */
    $account = $this->entity;
    // This form is used for two cases:
    // - Self-register (route = 'user.register').
    // - Admin-create (route = 'user.admin_create').
    // If the current user has permission to create users then it must be the
    // second case.
    $admin = $account->access('create');
    // Pass access information to the submit handler. Running an access check
    // inside the submit function interferes with form processing and breaks
    // hook_form_alter().
    $form['administer_users'] = [
        '#type' => 'value',
        '#value' => $admin,
    ];
    $form['#attached']['library'][] = 'core/drupal.form';
    // For non-admin users, populate the form fields using data from the
    // browser.
    if (!$admin) {
        $form['#attributes']['data-user-info-from-browser'] = TRUE;
    }
    // Because the user status has security implications, users are blocked by
    // default when created programmatically and need to be actively activated
    // if needed. When administrators create users from the user interface,
    // however, we assume that they should be created as activated by default.
    if ($admin) {
        $account->activate();
    }
    // Start with the default user account fields.
    $form = parent::form($form, $form_state);
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.