You are here

public function SettingsForm::buildForm in Slack 8

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/SettingsForm.php, line 37

Class

SettingsForm
Class SettingsForm.

Namespace

Drupal\slack\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('slack.settings');
  $form['slack_webhook_url'] = [
    '#type' => 'url',
    '#title' => $this
      ->t('Webhook URL'),
    '#description' => $this
      ->t('Enter your Webhook URL from an Incoming WebHooks integration. It looks like https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ'),
    '#default_value' => $config
      ->get('slack_webhook_url'),
    '#required' => TRUE,
  ];
  $form['slack_channel'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Default channel'),
    '#description' => $this
      ->t('Enter your channel name with # symbol, for example #general (or @username for a private message or a private group name).'),
    '#default_value' => $config
      ->get('slack_channel'),
  ];
  $form['slack_username'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Default username'),
    '#description' => $this
      ->t('What would you like to name your Slack bot?'),
    '#default_value' => $config
      ->get('slack_username'),
  ];
  $form['slack_icon_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Type of image'),
    '#options' => [
      'emoji' => $this
        ->t('Emoji'),
      'image' => $this
        ->t('Image'),
      'none' => $this
        ->t('None (Use default integration settings)'),
    ],
    '#default_value' => $config
      ->get('slack_icon_type'),
  ];
  $form['slack_icon_emoji'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Emoji code'),
    '#default_value' => $config
      ->get('slack_icon_emoji'),
    '#description' => $this
      ->t('What emoji would you use for your SlackBot?'),
    '#states' => [
      'visible' => [
        ':input[name="slack_icon_type"]' => [
          'value' => 'emoji',
        ],
      ],
    ],
  ];
  $form['slack_icon_url'] = [
    '#type' => 'url',
    '#title' => $this
      ->t('Image URL'),
    '#default_value' => $config
      ->get('slack_icon_url'),
    '#description' => $this
      ->t('What icon would you use for your SlackBot?'),
    '#states' => [
      'visible' => [
        ':input[name="slack_icon_type"]' => [
          'value' => 'image',
        ],
      ],
    ],
  ];
  if (empty($config
    ->get('slack_webhook_url'))) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('Slack sending message page will be available after you fill "Webhook URL" field'));
  }
  return parent::buildForm($form, $form_state);
}