You are here

public function CookiebotForm::buildForm in Cookiebot - Cookie consent, Cookie monitoring and Cookie control 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/CookiebotForm.php, line 91

Class

CookiebotForm
Cookiebot settings form.

Namespace

Drupal\cookiebot\Form

Code

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);
}