You are here

public function PopupMessageSettingsForm::buildForm in Popup message 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/PopupMessageSettingsForm.php, line 81

Class

PopupMessageSettingsForm
Class PopupMessageSettingsForm.

Namespace

Drupal\popup_message\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('popup_message.settings');
  $form['popup_message_enable'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Enable Popup'),
    '#default_value' => $config
      ->get('enable') ? $config
      ->get('enable') : 0,
    '#options' => [
      1 => $this
        ->t('Enabled'),
      0 => $this
        ->t('Disabled'),
    ],
  ];
  $form['popup_message_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Popup message settings'),
    '#collapsed' => FALSE,
    '#collapsible' => TRUE,
  ];
  $form['popup_message_fieldset']['popup_message_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Message title'),
    '#required' => TRUE,
    '#default_value' => $config
      ->get('title'),
  ];
  $popup_message_body = $config
    ->get('body');
  $form['popup_message_fieldset']['popup_message_body'] = [
    '#type' => 'text_format',
    '#base_type' => 'textarea',
    '#title' => $this
      ->t('Message body'),
    '#default_value' => $popup_message_body['value'],
    '#format' => isset($popup_message_body['format']) ? $popup_message_body['format'] : NULL,
  ];
  $form['popup_message_fieldset']['settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Settings'),
    '#expanded' => FALSE,
  ];
  $form['popup_message_fieldset']['cookie_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Cookie settings'),
    '#expanded' => FALSE,
  ];
  $form['popup_message_fieldset']['settings']['popup_message_width'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Window width'),
    '#required' => TRUE,
    '#default_value' => empty($config
      ->get('width')) ? 300 : $config
      ->get('width'),
  ];
  $form['popup_message_fieldset']['settings']['popup_message_height'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Window height'),
    '#required' => TRUE,
    '#default_value' => empty($config
      ->get('height')) ? 300 : $config
      ->get('height'),
  ];
  $form['popup_message_fieldset']['cookie_settings']['popup_message_check_cookie'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Check cookie'),
    '#description' => $this
      ->t('If enabled message will be displayed only once per browser session'),
    '#default_value' => $config
      ->get('check_cookie') ? $config
      ->get('check_cookie') : 0,
    '#options' => [
      1 => $this
        ->t('Enabled'),
      0 => $this
        ->t('Disabled'),
    ],
  ];
  $form['popup_message_fieldset']['cookie_settings']['popup_message_expire'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Cookie lifetime in days'),
    '#description' => $this
      ->t('Define lifetime of the cookie in days. Message will not reappear until the expiration time is exceeded. If 0, popup will reappear if browser has been closed.'),
    '#default_value' => $config
      ->get('expire') ? $config
      ->get('expire') : 0,
  ];
  $form['popup_message_fieldset']['settings']['popup_message_delay'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Delay'),
    '#description' => $this
      ->t('Message will show after this number of seconds. Set to 0 to show instantly.'),
    '#default_value' => $config
      ->get('delay') ? $config
      ->get('delay') : 0,
  ];
  $form['popup_message_fieldset']['settings']['popup_message_close_delay'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Delay before auto close'),
    '#description' => $this
      ->t('Message will close after this number of seconds. Set to 0 to disable it.'),
    '#default_value' => $config
      ->get('close_delay') ? $config
      ->get('close_delay') : 0,
  ];

  // Styles.
  // Find styles in module directory.
  $directory = drupal_get_path('module', 'popup_message') . '/styles';
  $subdirectories = scandir($directory);
  $styles = [];
  foreach ($subdirectories as $subdir) {
    if (is_dir($directory . '/' . $subdir)) {
      if (file_exists($directory . '/' . $subdir . '/' . POPUP_MESSAGE_CSS_NAME)) {
        $lib_path = $subdir . '/' . POPUP_MESSAGE_CSS_NAME;
        $styles[$lib_path] = $subdir;
      }
    }
  }
  $form['popup_message_fieldset']['settings']['popup_message_cover_opacity'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Background opacity (%)'),
    '#min' => 0,
    '#max' => 100,
    '#step' => 5,
    '#default_value' => $config
      ->get('cover_opacity') ?? 70,
    '#description' => $this
      ->t('Allows to set a custom background opacity value in percentage (0-100%).'),
  ];
  $form['popup_message_fieldset']['settings']['popup_message_style'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Popup style'),
    '#default_value' => empty($config
      ->get('style')) ? 0 : $config
      ->get('style'),
    '#options' => $styles,
    '#description' => $this
      ->t('To add custom styles create directory and file "modules/popup_message/popup_message_styles/custom_style/popup.css" and set in this file custom CSS code.'),
  ];
  $form['popup_message_fieldset']['visibility']['path'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Pages'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'visibility',
    '#weight' => 0,
  ];
  $options = [
    $this
      ->t('All pages except those listed'),
    $this
      ->t('Only the listed pages'),
  ];
  $description = $this
    ->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
    '%blog' => 'blog',
    '%blog-wildcard' => 'blog/*',
    '%front' => '<front>',
  ]);
  $title = $this
    ->t('Pages');
  $form['popup_message_fieldset']['visibility']['path']['popup_message_visibility'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Show block on specific pages'),
    '#options' => $options,
    '#default_value' => $config
      ->get('visibility') ? $config
      ->get('visibility') : 0,
  ];
  $form['popup_message_fieldset']['visibility']['path']['popup_message_visibility_pages'] = [
    '#type' => 'textarea',
    '#default_value' => $config
      ->get('visibility_pages') ? $config
      ->get('visibility_pages') : '',
    '#description' => $description,
    '#title' => '<span class="element-invisible">' . $title . '</span>',
  ];
  return parent::buildForm($form, $form_state);
}