You are here

public function CaptchaQuestionsSettingsForm::submitForm in Captcha Questions 8

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 ConfigFormBase::submitForm

File

src/Form/CaptchaQuestionsSettingsForm.php, line 242

Class

CaptchaQuestionsSettingsForm
Displays the captcha_questions settings form.

Namespace

Drupal\captcha_questions\Form

Code

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

  // If question, answer and description are all empty, delete the variables.
  // The last variable, captcha_questions_form_ids are removed on uninstall.
  if (empty($form_state
    ->getValue('captcha_questions_question')) && empty($form_state
    ->getValue('captcha_questions_answer'))) {
    $this
      ->config('captcha_questions.settings')
      ->delete('captcha_questions_question');
    $this
      ->config('captcha_questions.settings')
      ->delete('captcha_questions_answer');
    $this
      ->config('captcha_questions.settings')
      ->delete('captcha_questions_description');
  }
  if (empty($form_state
    ->getValue('captcha_questions_question')) && empty($form_state
    ->getValue('captcha_questions_answers'))) {
    $this
      ->config('captcha_questions.settings')
      ->delete('captcha_questions_question');
    $this
      ->config('captcha_questions.settings')
      ->delete('captcha_questions_answers');
    $this
      ->config('captcha_questions.settings')
      ->delete('captcha_questions_description');
  }

  // Split answers into arrays and set variable manually.
  $answers = explode("\n", $form_state
    ->getValue('captcha_questions_answers'));
  $answers = array_map('trim', $answers);
  asort($answers);
  $this
    ->config('captcha_questions.settings')
    ->set('captcha_questions_answers', $answers);
  $form_state
    ->unsetValue('captcha_questions_answers');
  $count_protected_form_ids = 0;

  // Counting number of selected form_ids, removing unselected form_ids.
  foreach ($form_state
    ->getValue('captcha_questions_form_ids') as $form_id => $value) {
    if ($form_id === $value) {
      $count_protected_form_ids++;
    }
    else {
      $captcha_questions_form_ids = $form_state
        ->getValue('captcha_questions_form_ids');
      unset($captcha_questions_form_ids[$form_id]);
      $form_state
        ->setValue('captcha_questions_form_ids', $captcha_questions_form_ids);
    }
  }
  if ($count_protected_form_ids == 0) {
    $this->messenger
      ->addMessage($this
      ->t('No forms selected'));
  }
  else {
    $message = $this
      ->formatPlural($count_protected_form_ids, '1 form protected', '@count forms protected');
    $this->messenger
      ->addMessage($message);
  }
  $this
    ->config('captcha_questions.settings')
    ->set('captcha_questions_watchdog', $form_state
    ->getValue('captcha_questions_watchdog'))
    ->set('captcha_questions_dblog', $form_state
    ->getValue('captcha_questions_dblog'))
    ->set('captcha_questions_question', $form_state
    ->getValue('captcha_questions_question'))
    ->set('captcha_questions_description', $form_state
    ->getValue('captcha_questions_description'))
    ->set('captcha_questions_form_ids', $form_state
    ->getValue('captcha_questions_form_ids'))
    ->set('add_form_name', $form_state
    ->getValue('add_form_name'))
    ->save();
  parent::submitForm($form, $form_state);
}