public function RocketChatSettingsForm::submitForm in Rocket.Chat 8.2
Same name and namespace in other branches
- 8 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 239 - Contains \Drupal\rocket_chat\Form\RocketChatSettingsForm.
Class
- RocketChatSettingsForm
- Class RocketChatSettingsForm.
Namespace
Drupal\rocket_chat\FormCode
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();
$this->messenger
->addStatus($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, $this->messenger);
$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'];
$this->messenger
->addStatus($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();
$this->messenger
->addStatus($this
->t('Updated the Rocketchat Admin User'));
}
if (!empty($form_secret)) {
$config
->clear('secret')
->set('secret', $form_secret)
->save();
$this->messenger
->addStatus($this
->t('Updated the Rocketchat Admin Password'));
}
}