Commit 896d5ddb authored by Sven Berg Ryen's avatar Sven Berg Ryen Committed by Sven Berg Ryen
Browse files

Issue #2985390 by Anybody, svenryen, jo.st, flefle, MrPaulDriver, upunkt,...

Issue #2985390 by Anybody, svenryen, jo.st, flefle, MrPaulDriver, upunkt, leymannx, bramvandenbulcke, gabrimonfa, Grayle, portulaca, anderz, RobbM: [1.x] Option to place Withdraw Button anywhere on site (e.g. in block)
parent 3bb56e1c
Loading
Loading
Loading
Loading
+10 −0
Changes for eu_cookie_compliance.links.menu.yml: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,16 @@ eu_cookie_compliance.settings:
  description: 'Configure the cookie consent popup.'
  route_name: eu_cookie_compliance.settings
  weight: -20
eu_cookie_compliance.toggle_cookie_settings_withdraw:
  title: "Cookie settings"
  description: "Toggle the cookie consent settings tray"
  url: "internal:#sliding-popup"
  menu_name: tools
  options:
    attributes:
      onclick: "if (Drupal.eu_cookie_compliance) { Drupal.eu_cookie_compliance.toggleWithdrawBanner(); } return false;"
      class:
        - eu-cookie-compliance-toggle-withdraw-banner
entity.cookie_category.collection:
  title: 'Categories'
  route_name: entity.cookie_category.collection
+1 −0
Changes for src/Form/EuCookieComplianceConfigForm.php: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -565,6 +565,7 @@ class EuCookieComplianceConfigForm extends ConfigFormBase {
    $form['withdraw_consent']['withdraw_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable floating privacy settings tab and withdraw consent banner'),
      '#description' => $this->t('Adds a floating privacy settings tab and withdraw consent banner. This option also provides a "Cookie settings" button block and menu item suggestion in "Navigation" menu.'),
      '#default_value' => $config->get('withdraw_enabled'),
    ];

+53 −0
Changes for src/Plugin/Block/CookieSettingsButtonBlock.php: 53 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

use Drupal\Core\Url;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;

/**
 * Provides a 'CookieSettingsButtonBlock' block.
 */
class CookieSettingsButtonBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    return AccessResult::allowedIfHasPermission($account, 'display eu cookie compliance popup');
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $config = \Drupal::config('eu_cookie_compliance.settings');
    if ($config->get('withdraw_enabled')) {
      return [
        '#type' => 'link',
        '#title' => $this->t('Cookie settings'),
        '#url' => Url::fromUserInput('#sliding-popup'),
        '#options' => [
          'attributes' => [
            'class' => [
              'eu-cookie-compliance-toggle-withdraw-banner',
              'button',
            ],
            'onclick' => 'if (Drupal.eu_cookie_compliance) { Drupal.eu_cookie_compliance.toggleWithdrawBanner(); } return false;',
          ],
        ],
      ];
    }
    return [
      '#title' => $this->t('Cookie settings'),
      '#markup' => $this->t('This block requires the "@withdraw_enabled_setting_name" to be enabled in <a href="@eu_cookie_compliance_settings_url">EU Cookie Compliance settings</a>.',
        [
          '@withdraw_enabled_setting_name' => $this->t('Enable floating privacy settings tab and withdraw consent banner'),
          '@eu_cookie_compliance_settings_url' => Url::fromRoute('eu_cookie_compliance.settings')->toString(),
        ]
      ),
    ];
  }

}