You are here

public function RocketChatSettingsForm::submitForm in Rocket.Chat 8

Same name and namespace in other branches
  1. 8.2 src/Form/RocketChatSettingsForm.php \Drupal\rocket_chat\Form\RocketChatSettingsForm::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/RocketChatSettingsForm.php, line 193
Contains \Drupal\rocket_chat\Form\RocketChatSettingsForm.

Class

RocketChatSettingsForm
Class RocketChatSettingsForm.

Namespace

Drupal\rocket_chat\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('rocket_chat.settings');
  $oldUrl = $config
    ->get('server');
  $form_url = $form_state
    ->getValue('url');
  $form_user = $form_state
    ->getValue('rocketchat_admin');
  $form_secret = $form_state
    ->getValue('rocketchat_key');
  if (!empty($form_url)) {
    $config
      ->clear('server')
      ->set('server', $form_url)
      ->save();
    drupal_set_message($this
      ->t('Updated the Rocketchat [@oldurl] -> [@url]', [
      '@url' => $form_url,
      "@oldurl" => empty($oldUrl) ? $this
        ->t("Not Set") : $oldUrl,
    ]));
    if (empty($form_user)) {
      $form_user = $config
        ->get('user');
    }
    if (empty($form_secret)) {
      $form_secret = $config
        ->get('secret');
    }
  }
  if (!empty($form_user) || !empty($form_secret)) {
    $apiConfig = new Drupal8Config($this
      ->configFactory(), $this->moduleHandler, $this->state);
    $user = empty($form_user) ? $config
      ->get('user') : $form_user;
    $secret = empty($form_secret) ? $config
      ->get('secret') : $form_secret;
    $memConfig = new InMemoryConfig($apiConfig, $user, $secret);
    $apiClient = new ApiClient($memConfig);
    $loginState = $apiClient
      ->login($user, $secret);
    if ($loginState) {
      $apiConfig
        ->setElement('rocket_chat_uid', $memConfig
        ->getElement('rocket_chat_uid', ""));
      $apiConfig
        ->setElement('rocket_chat_uit', $memConfig
        ->getElement('rocket_chat_uit', ""));
      $user = $apiClient
        ->whoAmI();
      $user['body']['username'];
      drupal_set_message($this
        ->t('Rocketchat User [@user]', [
        '@user' => $user['body']['username'],
      ]));
    }
    else {

      // Login failed, unset the credentials.
      $form_user = NULL;
      $form_secret = NULL;
    }
  }
  if (!empty($form_user)) {
    $config
      ->clear('user')
      ->set('user', $form_user)
      ->save();
    drupal_set_message($this
      ->t('Updated the Rocketchat Admin User'));
  }
  if (!empty($form_secret)) {
    $config
      ->clear('secret')
      ->set('secret', $form_secret)
      ->save();
    drupal_set_message($this
      ->t('Updated the Rocketchat Admin Password'));
  }
}