/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $user = $this->currentUser(); /** @var \Drupal\user\UserInterface $account */ $account = $this->entity; $admin = $user->hasPermission('administer users'); // 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'] = array('#type' => 'value', '#value' => $admin); // If we aren't admin but already logged on, go to the user page instead. if (!$admin && $user->isAuthenticated()) { return new RedirectResponse($this->url('entity.user.canonical', ['user' => \Drupal::currentUser()->id()], array('absolute' => TRUE))); } $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, $account); return $form; }
/** * Overrides Drupal\Core\Entity\EntityForm::form(). */ public function form(array $form, array &$form_state) { $user = $this->currentUser(); /** @var \Drupal\user\UserInterface $account */ $account = $this->entity; $admin = $user->hasPermission('administer users'); // 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'] = array('#type' => 'value', '#value' => $admin); // If we aren't admin but already logged on, go to the user page instead. if (!$admin && $user->isAuthenticated()) { return new RedirectResponse(url('user/' . \Drupal::currentUser()->id(), array('absolute' => TRUE))); } $form['#attached']['library'][] = 'core/jquery.cookie'; $form['#attributes']['class'][] = 'user-info-from-cookie'; // 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, $account); if ($admin) { // Redirect back to page which initiated the create request; usually // admin/people/create. $form_state['redirect'] = current_path(); } return $form; }