You are here

class CookiebotForm in Cookiebot - Cookie consent, Cookie monitoring and Cookie control 8

Cookiebot settings form.

Hierarchy

Expanded class hierarchy of CookiebotForm

1 string reference to 'CookiebotForm'
cookiebot.routing.yml in ./cookiebot.routing.yml
cookiebot.routing.yml

File

src/Form/CookiebotForm.php, line 17

Namespace

Drupal\cookiebot\Form
View 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

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
CookiebotForm::$aliasManager protected property Alias manager.
CookiebotForm::$cacheTagsInvalidator private property The cache tag invalidator service.
CookiebotForm::$entityTypeManager protected property Entity type manager.
CookiebotForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
CookiebotForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
CookiebotForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
CookiebotForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CookiebotForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
CookiebotForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
CookiebotForm::__construct public function Constructs a object. Overrides ConfigFormBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.