You are here

public function TwitterAccountsForm::submitForm in Tweet Feed 4.x

Same name and namespace in other branches
  1. 8.3 src/Form/TwitterAccountsForm.php \Drupal\tweet_feed\Form\TwitterAccountsForm::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/TwitterAccountsForm.php, line 118

Class

TwitterAccountsForm
Form controller for Tweet entity edit forms.

Namespace

Drupal\tweet_feed\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $config = $this
    ->config('tweet_feed.twitter_accounts');
  $accounts = $config
    ->get('accounts');
  $values = $form_state
    ->getValues();

  // If we're updating, then do things differently
  if (empty($values['account_update'])) {
    $account_machine_name = preg_replace('/[^a-z0-9]+/', '_', strtolower($values['account_name']));
    if (!empty($accounts[$account_machine_name])) {
      $suffix = 1;
      do {
        $new_account_machine_name = $account_machine_name . '_' . $suffix;
        $suffix++;
      } while (!empty($accounts[$new_account_machine_name]));
      $account_machine_name = $new_account_machine_name;
    }
    if (empty($accounts[$account_machine_name])) {
      $accounts[$account_machine_name] = [];
    }
    else {
      $account_machine_name = $values['account_machine_name'];
    }
  }
  else {
    $account_machine_name = $values['account_machine_name'];
  }
  $accounts[$account_machine_name]['account_name'] = $values['account_name'];
  $accounts[$account_machine_name]['consumer_key'] = $values['consumer_key'];
  $accounts[$account_machine_name]['consumer_secret'] = $values['consumer_secret'];
  $accounts[$account_machine_name]['oauth_token'] = $values['oauth_token'];
  $accounts[$account_machine_name]['oauth_token_secret'] = $values['oauth_token_secret'];
  $this
    ->config('tweet_feed.twitter_accounts')
    ->set('accounts', $accounts)
    ->save();
  $url = Url::fromRoute('tweet_feed.twitter_accounts');
  $form_state
    ->setRedirectUrl($url);
}