You are here

PopupSettingsForm.php in Popup Dialog 8

Same filename and directory in other branches
  1. 8.2 src/Form/PopupSettingsForm.php

File

src/Form/PopupSettingsForm.php
View source
<?php

namespace Drupal\popup_dialog\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class PopupSettingsForm.
 *
 * @package Drupal\popup_dialog\Form
 */
class PopupSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'popup_dialog.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'popup_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('popup_dialog.settings');
    $form['popup_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enabled'),
      '#description' => $this
        ->t('Popup will be enabled when the checkbox is active.'),
      '#default_value' => $config
        ->get('popup_enabled'),
    );
    $form['popup_settings'] = array(
      '#type' => 'details',
      '#title' => $this
        ->t('Popup Box Settings'),
      '#open' => TRUE,
    );
    $form['popup_settings']['popup_box_title'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Title'),
      '#description' => $this
        ->t('The title for the popup dialog box.'),
      '#maxlength' => 64,
      '#size' => 64,
      '#default_value' => $config
        ->get('popup_box_title'),
    );
    $form['popup_settings']['popup_box_body'] = [
      '#type' => 'text_format',
      '#title' => $this
        ->t('Body'),
      '#default_value' => $config
        ->get('popup_box_body')['value'],
      '#format' => $config
        ->get('popup_box_body')['format'],
    ];
    $form['popup_settings']['delay'] = array(
      '#type' => 'number',
      '#title' => $this
        ->t('Delay'),
      '#description' => $this
        ->t('Show message after the enter number of seconds. Enter 0 to show instantly.'),
      '#default_value' => $config
        ->get('delay'),
    );
    $form['popup_settings']['popup_top_position'] = array(
      '#type' => 'number',
      '#title' => $this
        ->t('Top Offset'),
      '#description' => $this
        ->t('Set the offset in px how much the popup box should be away from the top edge of the screen.'),
      '#default_value' => $config
        ->get('popup_top_position'),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this
      ->config('popup_dialog.settings')
      ->set('popup_enabled', $form_state
      ->getValue('popup_enabled'))
      ->set('popup_box_title', $form_state
      ->getValue('popup_box_title'))
      ->set('popup_box_body', $form_state
      ->getValue('popup_box_body'))
      ->set('delay', $form_state
      ->getValue('delay'))
      ->set('popup_top_position', $form_state
      ->getValue('popup_top_position'))
      ->save();
  }

}

Classes

Namesort descending Description
PopupSettingsForm Class PopupSettingsForm.