You are here

public function LearningPathCreateUserForm::submitForm in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Form/LearningPathCreateUserForm.php \Drupal\opigno_learning_path\Form\LearningPathCreateUserForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/LearningPathCreateUserForm.php, line 134

Class

LearningPathCreateUserForm
Members create form.

Namespace

Drupal\opigno_learning_path\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $name = $form_state
    ->getValue('name');
  $email = $form_state
    ->getValue('email');

  // Create new user.
  $lang = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $user = User::create();
  $user
    ->enforceIsNew();
  $user
    ->setUsername($name);
  $user
    ->setPassword(user_password());
  $user
    ->setEmail($email);
  $user
    ->set('init', $email);
  $user
    ->set('langcode', $lang);
  $user
    ->set('preferred_langcode', $lang);
  $user
    ->set('preferred_admin_langcode', $lang);
  if ($user
    ->hasField('field_created_by')) {
    $user
      ->set('field_created_by', [
      'target_id' => \Drupal::currentUser()
        ->id(),
    ]);
  }
  $user
    ->activate();
  $user
    ->save();

  // Notify user for creating account.
  _user_mail_notify('register_admin_created', $user);

  // Assign the user to the learning path.
  $group = $this
    ->getRequest()
    ->get('group');
  if ($group !== NULL) {
    $group
      ->addMember($user);
  }
  $this
    ->messenger()
    ->addMessage($this
    ->t('The new user has been created'));
}