You are here

public function ConfigForm::submitForm in GatherContent 8

Same name and namespace in other branches
  1. 8.5 src/Form/ConfigForm.php \Drupal\gathercontent\Form\ConfigForm::submitForm()
  2. 8.3 src/Form/ConfigForm.php \Drupal\gathercontent\Form\ConfigForm::submitForm()
  3. 8.4 src/Form/ConfigForm.php \Drupal\gathercontent\Form\ConfigForm::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 ConfigFormBase::submitForm

File

src/Form/ConfigForm.php, line 118

Class

ConfigForm
Class ConfigForm.

Namespace

Drupal\gathercontent\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();
  if ($triggering_element['#id'] === 'edit-submit') {
    if (!$form_state
      ->hasValue('account')) {
      $this
        ->config('gathercontent.settings')
        ->set('gathercontent_username', $form_state
        ->getValue('gathercontent_username'))
        ->set('gathercontent_api_key', $form_state
        ->getValue('gathercontent_api_key'))
        ->save();
      $form_state
        ->setSubmitted()
        ->setRebuild();
    }
    else {
      $submitted_account_id = $form_state
        ->getValue('account');
      $account_obj = new Account();
      $data = $account_obj
        ->getAccounts();
      foreach ($data as $account) {
        if ($account->id == $submitted_account_id) {
          $account_name = $account->name;
          $this
            ->config('gathercontent.settings')
            ->set('gathercontent_account', serialize(array(
            $submitted_account_id => $account_name,
          )))
            ->save();
          drupal_set_message(t("Credentials and project were saved."));
          $this
            ->config('gathercontent.settings')
            ->set('gathercontent_urlkey', $account->slug)
            ->save();
          break;
        }
      }
    }
  }
  elseif ($triggering_element['#id'] === 'edit-reset') {
    $this
      ->config('gathercontent.settings')
      ->clear('gathercontent_username')
      ->save();
    $this
      ->config('gathercontent.settings')
      ->clear('gathercontent_api_key')
      ->save();
    $this
      ->config('gathercontent.settings')
      ->clear('gathercontent_account')
      ->save();
    $this
      ->config('gathercontent.settings')
      ->clear('gathercontent_urlkey')
      ->save();
  }
}