RegisterForm.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Drupal\user;
  3. use Drupal\Core\Form\FormStateInterface;
  4. /**
  5. * Form handler for the user register forms.
  6. *
  7. * @internal
  8. */
  9. class RegisterForm extends AccountForm {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function form(array $form, FormStateInterface $form_state) {
  14. $user = $this->currentUser();
  15. /** @var \Drupal\user\UserInterface $account */
  16. $account = $this->entity;
  17. $admin = $user->hasPermission('administer users');
  18. // Pass access information to the submit handler. Running an access check
  19. // inside the submit function interferes with form processing and breaks
  20. // hook_form_alter().
  21. $form['administer_users'] = [
  22. '#type' => 'value',
  23. '#value' => $admin,
  24. ];
  25. $form['#attached']['library'][] = 'core/drupal.form';
  26. // For non-admin users, populate the form fields using data from the
  27. // browser.
  28. if (!$admin) {
  29. $form['#attributes']['data-user-info-from-browser'] = TRUE;
  30. }
  31. // Because the user status has security implications, users are blocked by
  32. // default when created programmatically and need to be actively activated
  33. // if needed. When administrators create users from the user interface,
  34. // however, we assume that they should be created as activated by default.
  35. if ($admin) {
  36. $account->activate();
  37. }
  38. // Start with the default user account fields.
  39. $form = parent::form($form, $form_state, $account);
  40. return $form;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function actions(array $form, FormStateInterface $form_state) {
  46. $element = parent::actions($form, $form_state);
  47. $element['submit']['#value'] = $this->t('Create new account');
  48. return $element;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function submitForm(array &$form, FormStateInterface $form_state) {
  54. $admin = $form_state->getValue('administer_users');
  55. if (!\Drupal::config('user.settings')->get('verify_mail') || $admin) {
  56. $pass = $form_state->getValue('pass');
  57. }
  58. else {
  59. $pass = user_password();
  60. }
  61. // Remove unneeded values.
  62. $form_state->cleanValues();
  63. $form_state->setValue('pass', $pass);
  64. $form_state->setValue('init', $form_state->getValue('mail'));
  65. parent::submitForm($form, $form_state);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function save(array $form, FormStateInterface $form_state) {
  71. $account = $this->entity;
  72. $pass = $account->getPassword();
  73. $admin = $form_state->getValue('administer_users');
  74. $notify = !$form_state->isValueEmpty('notify');
  75. // Save has no return value so this cannot be tested.
  76. // Assume save has gone through correctly.
  77. $account->save();
  78. $form_state->set('user', $account);
  79. $form_state->setValue('uid', $account->id());
  80. $this->logger('user')->notice('New user: %name %email.', ['%name' => $form_state->getValue('name'), '%email' => '<' . $form_state->getValue('mail') . '>', 'type' => $account->link($this->t('Edit'), 'edit-form')]);
  81. // Add plain text password into user account to generate mail tokens.
  82. $account->password = $pass;
  83. // New administrative account without notification.
  84. if ($admin && !$notify) {
  85. $this->messenger()->addStatus($this->t('Created a new user account for <a href=":url">%name</a>. No email has been sent.', [':url' => $account->url(), '%name' => $account->getUsername()]));
  86. }
  87. // No email verification required; log in user immediately.
  88. elseif (!$admin && !\Drupal::config('user.settings')->get('verify_mail') && $account->isActive()) {
  89. _user_mail_notify('register_no_approval_required', $account);
  90. user_login_finalize($account);
  91. $this->messenger()->addStatus($this->t('Registration successful. You are now logged in.'));
  92. $form_state->setRedirect('<front>');
  93. }
  94. // No administrator approval required.
  95. elseif ($account->isActive() || $notify) {
  96. if (!$account->getEmail() && $notify) {
  97. $this->messenger()->addStatus($this->t('The new user <a href=":url">%name</a> was created without an email address, so no welcome message was sent.', [':url' => $account->url(), '%name' => $account->getUsername()]));
  98. }
  99. else {
  100. $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
  101. if (_user_mail_notify($op, $account)) {
  102. if ($notify) {
  103. $this->messenger()->addStatus($this->t('A welcome message with further instructions has been emailed to the new user <a href=":url">%name</a>.', [':url' => $account->url(), '%name' => $account->getUsername()]));
  104. }
  105. else {
  106. $this->messenger()->addStatus($this->t('A welcome message with further instructions has been sent to your email address.'));
  107. $form_state->setRedirect('<front>');
  108. }
  109. }
  110. }
  111. }
  112. // Administrator approval required.
  113. else {
  114. _user_mail_notify('register_pending_approval', $account);
  115. $this->messenger()->addStatus($this->t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your email address.'));
  116. $form_state->setRedirect('<front>');
  117. }
  118. }
  119. }