class RocketChatSettingsForm in Rocket.Chat 8
Same name and namespace in other branches
- 8.2 src/Form/RocketChatSettingsForm.php \Drupal\rocket_chat\Form\RocketChatSettingsForm
Class RocketChatSettingsForm.
@package Drupal\rocket_chat\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\rocket_chat\Form\RocketChatSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of RocketChatSettingsForm
1 string reference to 'RocketChatSettingsForm'
File
- src/
Form/ RocketChatSettingsForm.php, line 51 - Contains \Drupal\rocket_chat\Form\RocketChatSettingsForm.
Namespace
Drupal\rocket_chat\FormView source
class RocketChatSettingsForm extends ConfigFormBase {
private $moduleHandler;
private $state;
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The ModuleHandler to interact with loaded modules.
* @param \Drupal\Core\State\StateInterface $state
* The Stateinterface to manipulate the state.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $moduleHandler, StateInterface $state) {
parent::__construct($config_factory);
$this->moduleHandler = $moduleHandler;
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
if (!empty($container)) {
return new static($container
->get("config.factory"), $container
->get("module_handler"), $container
->get("state"));
}
else {
// Something huge went wrong, we are missing the ContainerInterface.
throw new ServiceNotFoundException('ContainerInterface');
}
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'rocket_chat.admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'rocket_chat.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$config = $this
->config('rocket_chat.settings');
$server = $config
->get('server');
$form['url'] = [
'#type' => 'url',
'#title' => $this
->t('The Rocket.chat server address:'),
'#required' => FALSE,
];
// Only set the value if there is a value.
if (!empty($server)) {
$form['url']['#defaultvalue'] = $server;
$form['url']['#attributes']['placeholder'] = $server;
}
else {
$form['url']['#attributes']['placeholder'] = "https://demo.rocket.chat/";
}
// Only add the following when the rocket_chat_api module is enabled.
if ($this->moduleHandler
->moduleExists('rocket_chat_api')) {
$form['rocketchat_admin'] = [
'#type' => 'password',
'#description' => $this
->t("Rocket chat Admin login name (for API use)"),
'#title' => $this
->t('Rocketchat Admin User:'),
'#required' => FALSE,
'#attributes' => [
'placeholder' => empty($config
->get('user')) ? "<Rocket chat Admin user name>" : $config
->get('user'),
],
];
$form['rocketchat_key'] = [
'#type' => 'password',
'#title' => $this
->t('Rocketchat Admin Password:'),
'#description' => $this
->t("Rocket chat Admin login password (for API use)"),
'#required' => FALSE,
'#attributes' => [
'placeholder' => '****************',
],
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// All required fields are submitted.
if (!empty($form_state
->getValue('url'))) {
// Check if host server is running.
$smokeCheck = Utility::serverRun($form_state
->getValue('url'));
$info = [];
if ($smokeCheck) {
$apiConfig = new Drupal8Config($this
->configFactory(), $this->moduleHandler, $this->state);
$empty = "";
$memConfig = new InMemoryConfig($apiConfig, $empty, $empty);
$memConfig
->setElement('rocket_chat_url', $form_state
->getValue('url'));
$apiClient = new ApiClient($memConfig);
// Check if the Rocket chat is actually functional with an info call.
$info = $apiClient
->info();
}
if (!$smokeCheck || !$info['status'] == "OK") {
$erred = TRUE;
}
else {
$erred = FALSE;
}
if ($erred) {
$form_state
->setErrorByName('url', "<div class=\"error rocketchat\">" . $this
->t('<strong>Server is not working!</strong><br>') . $this
->t('<em>incorrect address</em>,') . ' ' . $this
->t('please check your supplied URL') . '</div>');
}
}
}
/**
* {@inheritdoc}
*/
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'));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
RocketChatSettingsForm:: |
private | property | ||
RocketChatSettingsForm:: |
private | property | ||
RocketChatSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
RocketChatSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
RocketChatSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
RocketChatSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
RocketChatSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
RocketChatSettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
RocketChatSettingsForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |