You are here

public function RocketChatSettingsForm::buildForm in Rocket.Chat 8.2

Same name and namespace in other branches
  1. 8 src/Form/RocketChatSettingsForm.php \Drupal\rocket_chat\Form\RocketChatSettingsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/RocketChatSettingsForm.php, line 130
Contains \Drupal\rocket_chat\Form\RocketChatSettingsForm.

Class

RocketChatSettingsForm
Class RocketChatSettingsForm.

Namespace

Drupal\rocket_chat\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
  $config = $this
    ->config('rocket_chat.settings');
  $server = $config
    ->get('server');
  $apiConfig = new Drupal8Config($this
    ->configFactory(), $this->moduleHandler, $this->state, $this->messenger);
  $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')) {
    $user = $config
      ->get('user');
    $form['rocketchat_admin'] = [
      '#type' => 'password',
      '#description' => $this
        ->t("Rocket chat Admin user name (for API use)"),
      '#title' => $this
        ->t('Rocketchat Admin User:'),
      '#required' => FALSE,
      '#attributes' => [
        'autocomplete' => "new-password",
        'placeholder' => empty($user) ? "(Rocket chat Admin user name)" : $user,
      ],
    ];
    if (!empty($user)) {
      $form['rocketchat_admin']['#defaultvalue'] = $user;
    }
    $form['rocketchat_key'] = [
      '#type' => 'password',
      '#title' => $this
        ->t('Rocketchat Admin Password:'),
      '#description' => $this
        ->t("Rocket chat Admin password (for API use)"),
      '#required' => FALSE,
      '#attributes' => [
        'autocomplete' => "new-password",
        'placeholder' => '****************',
      ],
    ];
  }
  $form['rocketchatRefreshCache'] = [
    '#type' => 'button',
    '#value' => $this
      ->t("Rebuild Rocketchat Cache"),
    "#submit" => [
      [
        '\\Drupal\\rocket_chat\\Form\\RocketChatSettingsForm',
        'altSubmitForm',
      ],
    ],
    "#limit_validation_errors" => FALSE,
    '#executes_submit_callback' => TRUE,
  ];
  if (!$apiConfig
    ->isReady()) {
    $form['rocketchatRefreshCache']['#disabled'] = TRUE;
  }
  if ($this->moduleHandler
    ->moduleExists("rocket_chat_group")) {
    $form['rocketchatRefreshGroups'] = [
      '#type' => 'button',
      '#value' => $this
        ->t("Rebuild Groups Channels"),
      "#submit" => [
        [
          '\\Drupal\\rocket_chat\\Form\\RocketChatSettingsForm',
          'groupsSubmitForm',
        ],
      ],
      "#limit_validation_errors" => FALSE,
      '#executes_submit_callback' => TRUE,
    ];
    if (!$apiConfig
      ->isReady()) {
      $form['rocketchatRefreshGroups']['#disabled'] = TRUE;
    }
  }
  return parent::buildForm($form, $form_state);
}