class CookiebotForm in Cookiebot - Cookie consent, Cookie monitoring and Cookie control 8
Cookiebot settings form.
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\cookiebot\Form\CookiebotForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of CookiebotForm
1 string reference to 'CookiebotForm'
File
- src/
Form/ CookiebotForm.php, line 17
Namespace
Drupal\cookiebot\FormView source
class CookiebotForm extends ConfigFormBase {
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* Alias manager.
*
* @var \Drupal\path_alias\AliasManagerInterface
*/
protected $aliasManager;
/**
* The cache tag invalidator service.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
*/
private $cacheTagsInvalidator;
/**
* Constructs a object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityTypeManager $entity_manager
* The entity type manager.
* @param \Drupal\path_alias\AliasManagerInterface $alias_manager
* The alias manager.
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
* The cache tag invalidator service.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManager $entity_manager, AliasManagerInterface $alias_manager, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
parent::__construct($config_factory);
$this
->setConfigFactory($config_factory);
$this->entityTypeManager = $entity_manager;
$this->aliasManager = $alias_manager;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('path_alias.manager'), $container
->get('cache_tags.invalidator'));
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'cookiebot.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'cookiebot_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('cookiebot.settings');
if (empty($config
->get('cookiebot_cbid'))) {
$this
->messenger()
->addWarning($this
->t('Cookiebot functionality is disabled until you enter a valid CBID.'));
}
$default_filter_format = filter_default_format();
$full_html_format = FilterFormat::load('full_html');
if ($default_filter_format == 'restricted_html' && !empty($full_html_format) && $full_html_format
->get('status')) {
$default_filter_format = 'full_html';
}
$form['cookiebot_cbid'] = [
'#type' => 'textfield',
'#title' => $this
->t('Your cookiebot Domain Group ID (CBID)'),
'#description' => $this
->t("This ID looks like 00000000-0000-0000-0000-000000000000. You can find it in the <a href='https://www.cookiebot.com/en/manage'>Cookiebot Manager</a> on the 'Your scripts' tab."),
'#default_value' => $config
->get('cookiebot_cbid'),
];
$form['cookiebot_block_cookies'] = [
'#type' => 'checkbox',
'#title' => t('Automatically block all cookies'),
'#description' => t('Defines if Cookiebot should <a href=":automatic_url">automatically block all cookies</a> until a user has consented. If not set, cookie-setting scripts should manually be marked up as described in the <a href=":manual_url">manual implementation guide</a>.', [
':automatic_url' => 'https://www.cookiebot.com/en/automatic-cookie-control/',
':manual_url' => 'https://www.cookiebot.com/en/manual-implementation/',
]),
'#default_value' => $config
->get('cookiebot_block_cookies'),
];
$form['cookiebot_iab_enabled'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enabling IAB framework'),
'#description' => $this
->t('IAB (Interactive Advertising Bureau) model puts scripts control in the hands of advertisers and vendors by only signaling consent to vendors. More information about <a href="https://support.cookiebot.com/hc/en-us/articles/360007652694-Cookiebot-and-the-IAB-Consent-Framework">Cookiebot and the IAB Consent Framework</a>.'),
'#default_value' => $config
->get('cookiebot_iab_enabled'),
];
$form['cookiebot_declaration'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Cookie declaration'),
];
$form['cookiebot_declaration']['cookiebot_show_declaration'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show the Cookiebot cookie declaration'),
'#description' => $this
->t('Automatically show the full Cookiebot cookie declaration on the given page.'),
'#default_value' => $config
->get('cookiebot_show_declaration'),
];
$form['visibility'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Cookiebot visibility'),
];
$form['visibility']['exclude_paths'] = [
'#type' => 'textarea',
'#title' => $this
->t('Exclude paths'),
'#default_value' => !empty($config
->get('exclude_paths')) ? $config
->get('exclude_paths') : '',
'#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>',
]),
];
$form['visibility']['exclude_admin_theme'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Exclude admin pages'),
'#default_value' => $config
->get('exclude_admin_theme'),
];
$form['visibility']['exclude_uid_1'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Don’t show the Cookiebot for UID 1.'),
'#default_value' => $config
->get('exclude_uid_1'),
];
$declaration_node = '';
$alias = $this->aliasManager
->getPathByAlias($config
->get('cookiebot_show_declaration_node_path'));
if (preg_match('/node\\/(\\d+)/', $alias, $matches)) {
$declaration_node = $this->entityTypeManager
->getStorage('node')
->load($matches[1]);
}
$description = $this
->t('Show the full cookie declaration on the node page with the given title.');
$description .= '<br />';
$description .= $this
->t("Note that custom templates and modules like Panels and Display Suite can prevent the declaration from showing up.\n You can always place our block or manually place Cookiebot's declaration script found in their manager if your input filters allow it.");
$form['cookiebot_declaration']['cookiebot_show_declaration_node_path'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#title' => $this
->t('Node page title'),
'#description' => $description,
'#default_value' => $declaration_node,
'#states' => [
'visible' => [
':input[name="cookiebot_show_declaration"]' => [
'checked' => TRUE,
],
],
],
];
$form['placeholders'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Placeholders for blocked elements with src attribute (iframe, etc.)'),
'#description' => $this
->t('Define placeholders for blocked ´src´ elements like iframe, img, audio, video, embed, picture, source. In automatic mode some sources like YouTube iFrames are blocked automatically. In manual mode you have to add the markup yourself. See Cookiebot support <a href=":url1" target="_blank">here</a> and <a href=":url2" target="_blank">here</a> for details.', [
':url1' => 'https://support.cookiebot.com/hc/en-us/articles/360003790854-Iframe-cookie-consent-with-YouTube-example',
':url2' => 'https://support.cookiebot.com/hc/en-us/articles/360003812053-Hide-and-show-content-based-on-the-visitor-s-consent',
]),
];
$form['placeholders']['marketing'] = [
'#type' => 'details',
'#title' => $this
->t('Marketing') . ' ' . '([data-src][data-cookieconsent="marketing"])',
'#description' => $this
->t('Blocked elements with [data-src][data-cookieconsent="marketing"] attributes. This is typically automatically added by Cookiebot in automatic mode.'),
];
$form['placeholders']['marketing']['message_placeholder_cookieconsent_optout_marketing_show'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show placeholder message for blocked marketing elements'),
'#description' => $this
->t('Select if you want to show a message for blocked elements like iframes, hidden by cookiebot until marketing consent is given.'),
'#default_value' => $config
->get('message_placeholder_cookieconsent_optout_marketing_show'),
];
$message_placeholder_cookieconsent_optout_marketing_format = $config
->get('message_placeholder_cookieconsent_optout_marketing.format');
if (!empty($message_placeholder_cookieconsent_optout_marketing_format)) {
$filter_format = FilterFormat::load($message_placeholder_cookieconsent_optout_marketing_format);
if (empty($filter_format) || !$filter_format
->get('status')) {
$message_placeholder_cookieconsent_optout_marketing_format = $default_filter_format;
}
}
$form['placeholders']['marketing']['message_placeholder_cookieconsent_optout_marketing'] = [
'#type' => 'text_format',
'#title' => $this
->t('Placebolder message for blocked marketing elements'),
'#default_value' => !empty($config
->get('message_placeholder_cookieconsent_optout_marketing.value')) ? $config
->get('message_placeholder_cookieconsent_optout_marketing.value') : 'Please <a href="!cookiebot_renew" class="cookieconsent-optout-marketing__cookiebot-renew">accept marketing-cookies</a> to view this embedded content from <a href="!cookiebot_from_src_url" target="_blank" class="cookieconsent-optout-marketing__from-src-url">!cookiebot_from_src_url</a>',
'#required' => FALSE,
'#description' => $this
->t("Add this placeholder below the blocked marketing element, if the user has not consented to marketing cookies.<br />Clear to use the default markup.<br />You may use these dynamical placeholders: <ul><li><em>!cookiebot_renew</em> = javascript:Cookiebot.renew()</li><li><em>!cookiebot_from_src_url</em> = iframe data-src attribute value</li></ul>"),
'#format' => $message_placeholder_cookieconsent_optout_marketing_format,
'#states' => [
'visible' => [
':input[name="message_placeholder_cookieconsent_optout_marketing_show"]' => [
'checked' => TRUE,
],
],
],
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$cbid_trimmed = trim($form_state
->getValue('cookiebot_cbid'));
$form_state
->setValue('cookiebot_cbid', $cbid_trimmed);
if (!empty($cbid_trimmed) && !preg_match('/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/', $cbid_trimmed)) {
$form_state
->setErrorByName('cookiebot_cbid', $this
->t('The entered Domain Group ID is not formatted correctly.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->cacheTagsInvalidator
->invalidateTags([
'cookiebot:cbid',
'cookiebot:show_declaration',
'cookiebot:iab_enabled',
]);
$this
->config('cookiebot.settings')
->set('cookiebot_cbid', $form_state
->getValue('cookiebot_cbid'))
->set('cookiebot_block_cookies', $form_state
->getValue('cookiebot_block_cookies'))
->set('cookiebot_iab_enabled', $form_state
->getValue('cookiebot_iab_enabled'))
->set('cookiebot_show_declaration', $form_state
->getValue('cookiebot_show_declaration'))
->set('cookiebot_show_declaration_node_path', $this->aliasManager
->getAliasByPath('/node/' . $form_state
->getValue('cookiebot_show_declaration_node_path')))
->set('exclude_paths', $form_state
->getValue('exclude_paths'))
->set('exclude_admin_theme', $form_state
->getValue('exclude_admin_theme'))
->set('exclude_uid_1', $form_state
->getValue('exclude_uid_1'))
->set('message_placeholder_cookieconsent_optout_marketing_show', $form_state
->getValue('message_placeholder_cookieconsent_optout_marketing_show'))
->set('message_placeholder_cookieconsent_optout_marketing', $form_state
->getValue('message_placeholder_cookieconsent_optout_marketing'))
->save();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
CookiebotForm:: |
protected | property | Alias manager. | |
CookiebotForm:: |
private | property | The cache tag invalidator service. | |
CookiebotForm:: |
protected | property | Entity type manager. | |
CookiebotForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
CookiebotForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
CookiebotForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
CookiebotForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
CookiebotForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
CookiebotForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
CookiebotForm:: |
public | function |
Constructs a object. Overrides ConfigFormBase:: |
|
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. | |
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. | |
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. |