You are here

public function MessageBannerSettingsForm::buildForm in Message Banner 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/MessageBannerSettingsForm.php, line 77

Class

MessageBannerSettingsForm
The message banner settings form.

Namespace

Drupal\message_banner\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) : array {
  $config = $this
    ->config('message_banner.settings');
  $form['banner_enabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable banner'),
    '#default_value' => $config
      ->get('banner_enabled') ?: FALSE,
  ];
  $form['message_banner'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Message banner settings'),
    '#states' => [
      'visible' => [
        ':input[name="banner_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['message_banner']['banner_enabled_on_admin_routes'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable banner on admin routes'),
    '#description' => $this
      ->t('If not checked, the banner will only appear on non-admin routes.'),
    '#default_value' => $config
      ->get('banner_enabled_on_admin_routes') ?: FALSE,
    '#states' => [
      'visible' => [
        ':input[name="banner_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['message_banner']['banner_show_again_minutes'] = [
    '#type' => 'number',
    '#step' => 1,
    '#title' => $this
      ->t('Minutes before showing banner after dismissing it'),
    '#default_value' => $config
      ->get('banner_show_again_minutes'),
    '#description' => $this
      ->t('The number of minutes to elapse after dimissing the banner before showing it again (0 to disable).'),
  ];
  $form['message_banner']['banner_color'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Banner color'),
    '#description' => $this
      ->t('Choose the background color for the banner.'),
    '#options' => $this
      ->getBannerColors(),
    '#default_value' => $config
      ->get('banner_color') ?: NULL,
    '#states' => [
      'required' => [
        ':input[name="banner_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['message_banner']['banner_text'] = [
    '#type' => 'text_format',
    '#title' => $this
      ->t('Message'),
    '#description' => $this
      ->t('This message will be shown to every site visitor, so make sure it does not contain any sensitive information!'),
    '#default_value' => $config
      ->get('banner_text.value') ?: '',
    '#format' => $config
      ->get('banner_text.format') ?: 'basic_html',
  ];
  return parent::buildForm($form, $form_state);
}