You are here

public function UserCsvImportForm::submitForm in User CSV import 8

Same name and namespace in other branches
  1. 2.0.x src/Form/UserCsvImportForm.php \Drupal\user_csv_import\Form\UserCsvImportForm::submitForm()
  2. 1.0.x src/Form/UserCsvImportForm.php \Drupal\user_csv_import\Form\UserCsvImportForm::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/UserCsvImportForm.php, line 227

Class

UserCsvImportForm
Provides methods to define and build the user import form.

Namespace

Drupal\user_csv_import\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Get form data.
  $file = $this->file[0];
  $roles = $form_state
    ->getValue([
    'config_options',
    'roles',
  ]);
  $fields = $form_state
    ->getValue([
    'config_fields',
    'fields',
  ]);

  // Construct data to send to the controller.
  $config = [
    'roles' => array_filter($roles, function ($item) {
      return $item;
    }),
    'fields' => array_filter($fields, function ($item) {
      return $item;
    }),
    'password' => $form_state
      ->getValue([
      'config_options',
      'password',
    ]),
    'status' => $form_state
      ->getValue([
      'config_options',
      'status',
    ]),
  ];

  // Return success message.
  if ($created = UserCsvImportController::processUpload($file, $config)) {
    $this->messenger
      ->addMessage($this
      ->t('Successfully imported @count users.', [
      '@count' => count($created),
    ]));
  }
  else {

    // Return error message.
    $this->messenger
      ->addMessage($this
      ->t('No users imported.'));
  }

  // Redirect to admin users page.
  $form_state
    ->setRedirectUrl(new Url('entity.user.collection'));
}