class SettingsPopup in General Data Protection Regulation Compliance 8
Implements the form controller.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\gdpr_compliance\Form\SettingsPopup
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SettingsPopup
1 string reference to 'SettingsPopup'
File
- src/
Form/ SettingsPopup.php, line 11
Namespace
Drupal\gdpr_compliance\FormView source
class SettingsPopup extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'gdpr_compliance';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'gdpr_compliance.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('gdpr_compliance.settings');
$form['general'] = [
'#type' => 'details',
'#title' => $this
->t('Display pop-up'),
'#open' => TRUE,
];
$form["general"]['popup-guests'] = [
'#title' => $this
->t('Display for guests'),
'#type' => 'checkbox',
'#default_value' => $config
->get('popup-guests'),
];
$form["general"]['popup-users'] = [
'#title' => $this
->t('Display for authenticated users'),
'#type' => 'checkbox',
'#default_value' => $config
->get('popup-users'),
];
$form["general"]['popup-position'] = [
'#title' => $this
->t('Popup position'),
'#type' => 'radios',
'#required' => TRUE,
'#options' => [
'top' => $this
->t('Top'),
'bottom' => $this
->t('Bottom'),
],
'#default_value' => $config
->get('popup-position'),
];
$form["general"]['popup-morelink'] = [
'#title' => $this
->t('Url for [More information] button.'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => $config
->get('popup-morelink'),
'#description' => $this
->t('Relative path starts with "/", or absolute start with http/https.'),
];
$form["text"] = [
'#type' => 'details',
'#title' => $this
->t('Text'),
'#open' => FALSE,
];
$defaults = [
'text-cookies' => $this
->t("We use cookies on our website to support technical features that enhance your user experience."),
'text-analytics' => $this
->t("We also use analytics & advertising services. To opt-out click for more information."),
'btn-agree' => $this
->t("I've read it"),
'btn-findmore' => $this
->t("More information"),
];
$form["text"]['popup-text-cookies'] = [
'#title' => $this
->t('Line 1 (cookies)'),
'#type' => 'textfield',
'#default_value' => $config
->get('popup-text-cookies'),
'#description' => $this
->t('Leave blank to use default: <i>@default</i>', [
'@default' => $defaults['text-cookies'],
]),
'#maxlength' => 255,
];
$form["text"]['popup-text-analytics'] = [
'#title' => $this
->t('Line 2 (analytics)'),
'#type' => 'textfield',
'#default_value' => $config
->get('popup-text-analytics'),
'#description' => $this
->t('Leave blank to use default: <i>@default</i>', [
'@default' => $defaults['text-analytics'],
]),
'#maxlength' => 255,
];
$form["text"]['popup-btn-agree'] = [
'#title' => $this
->t('Agree button'),
'#type' => 'textfield',
'#default_value' => $config
->get('popup-btn-agree'),
'#description' => $this
->t('Leave blank to use default: <i>@default</i>', [
'@default' => $defaults['btn-agree'],
]),
];
$form["text"]['popup-btn-findmore'] = [
'#title' => $this
->t('Find-more button'),
'#type' => 'textfield',
'#default_value' => $config
->get('popup-btn-findmore'),
'#description' => $this
->t('Leave blank to use default: <i>@default</i>', [
'@default' => $defaults['btn-findmore'],
]),
];
$form['color'] = [
'#type' => 'details',
'#title' => $this
->t('Color'),
'#open' => TRUE,
];
$form['color']['popup-custom-color'] = [
'#title' => $this
->t('Use custom colors'),
'#type' => 'checkbox',
'#default_value' => $config
->get('popup-custom-color'),
'#description' => $this
->t('Set color by color-widget of use hex value.'),
];
$form['color']['popup-info'] = [
'#markup' => '<h3>' . $this
->t('Pop-up background color:') . '</h3>',
];
$form["color"]['popup-color'] = [
'#title' => $this
->t('Color'),
'#type' => 'color',
'#default_value' => $config
->get('popup-color'),
'#description' => $this
->t('Text will be inversed. Now text is @color.', [
'@color' => $config
->get('popup-text'),
]),
];
$form["color"]['popup-hex'] = [
'#title' => $this
->t('Color HEX'),
'#type' => 'textfield',
'#placeholder' => $config
->get('popup-color'),
'#description' => $this
->t('Background has opacity 0.9'),
];
$form['color']['button-info'] = [
'#markup' => '<h3>' . $this
->t('Button color:') . '</h3>',
];
$form["color"]['button-color'] = [
'#title' => $this
->t('Color'),
'#type' => 'color',
'#default_value' => $config
->get('button-color'),
];
$form["color"]['button-hex'] = [
'#title' => $this
->t('Color HEX'),
'#type' => 'textfield',
'#placeholder' => $config
->get('button-color'),
'#description' => $this
->t('Text will be inversed. Now text is @color.', [
'@color' => $config
->get('button-text'),
]),
];
return parent::buildForm($form, $form_state);
}
/**
* Implements a form submit handler.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('gdpr_compliance.settings');
$popup_color = $form_state
->getValue('popup-color');
if ($popup_hex = $this
->tryHex($form_state
->getValue('popup-hex'))) {
$popup_color = $popup_hex;
}
$button_color = $form_state
->getValue('button-color');
if ($button_hex = $this
->tryHex($form_state
->getValue('button-hex'))) {
$button_color = $button_hex;
}
$config
->set('popup-users', $form_state
->getValue('popup-users'))
->set('popup-guests', $form_state
->getValue('popup-guests'))
->set('popup-position', $form_state
->getValue('popup-position'))
->set('popup-morelink', $form_state
->getValue('popup-morelink'))
->set('popup-text-cookies', $form_state
->getValue('popup-text-cookies'))
->set('popup-text-analytics', $form_state
->getValue('popup-text-analytics'))
->set('popup-btn-agree', $form_state
->getValue('popup-btn-agree'))
->set('popup-btn-findmore', $form_state
->getValue('popup-btn-findmore'))
->set('popup-custom-color', $form_state
->getValue('popup-custom-color'))
->set('popup-color', $popup_color)
->set('popup-text', $this
->getColorContrastInverse($popup_color))
->set('button-color', $button_color)
->set('button-text', $this
->getColorContrastInverse($button_color))
->save();
}
/**
* Try hex.
*/
private function tryHex($color) {
$result = FALSE;
$hex = str_replace('#', '', trim($color));
if (strlen($hex) == 6 && ctype_xdigit($hex)) {
$result = "#{$hex}";
}
return $result;
}
/**
* Inverse check.
*/
private function getColorContrastInverse($color) {
if ($color == 'none') {
$color = '#306133';
}
$hexcolor = substr($color, 1);
$r = hexdec(substr($hexcolor, 0, 2));
$g = hexdec(substr($hexcolor, 2, 2));
$b = hexdec(substr($hexcolor, 4, 2));
$yiq = ($r * 299 + $g * 587 + $b * 114) / 1000;
return $yiq >= 128 ? 'black' : 'white';
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SettingsPopup:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SettingsPopup:: |
private | function | Inverse check. | |
SettingsPopup:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SettingsPopup:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SettingsPopup:: |
public | function |
Implements a form submit handler. Overrides ConfigFormBase:: |
|
SettingsPopup:: |
private | function | Try hex. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |