View source
<?php
namespace Drupal\popup_dialog\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class PopupSettingsForm extends ConfigFormBase {
protected function getEditableConfigNames() {
return [
'popup_dialog.settings',
];
}
public function getFormId() {
return 'popup_settings';
}
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);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
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();
}
}