Commit f84b4086 authored by Arne Jørgensen's avatar Arne Jørgensen Committed by Bram Driesen
Browse files

Issue #3071334 by arnested, akanksha-hp, a.milkovsky, BramDriesen, c960657:...

Issue #3071334 by arnested, akanksha-hp, a.milkovsky, BramDriesen, c960657: Allow to set the language and multilingual support
parent 3dafeb53
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -28,6 +28,16 @@ function hook_cookiebot_path_match_alter(&$excluded, $path, $exclude_paths) {
  }
}

/**
 * Alter the cookiebot culture langcode
 *
 * @param array $cookiebot
 *   The cookiebot build array.
 */
function hook_cookiebot_culture_alter(&$cookiebot) {

}

/**
 * @} End of "addtogroup hooks".
 */
+15 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ function cookiebot_theme($existing, $type, $theme, $path) {
    'cookiebot_declaration' => [
      'variables' => [
        'cookiebot_src' => NULL,
        'cookiebot_culture' => NULL,
      ],
    ],
    'cookiebot_blocked_element_placeholder' => [
@@ -124,6 +125,13 @@ function cookiebot_page_attachments_alter(array &$page) {
    $cookiebot['#attributes']['data-framework'] = 'IAB';
  }

  $culture = $config->get('cookiebot_drupal_culture');
  if ($culture) {
    $cookiebot_culture = \Drupal::languageManager()->getCurrentLanguage()->getId();
    \Drupal::moduleHandler()->alter('cookiebot_culture', $cookiebot_culture);
    $cookiebot['#attributes']['data-culture'] = $cookiebot_culture;
  }

  // Attach the Cookiebot library.
  $page['#attached']['html_head'][] = [$cookiebot, 'cookiebot'];
  $message_placeholder_cookieconsent_optout_marketing_build = [
@@ -159,9 +167,16 @@ function cookiebot_node_view_alter(array &$build) {

  // Print full cookies declaration from Cookiebot.
  if ($current_alias === $declaration_node_path) {
    $culture = $config->get('cookiebot_drupal_culture');
    $cookiebot_culture = $culture ? \Drupal::languageManager()->getCurrentLanguage()->getId() : NULL;
    if ($cookiebot_culture) {
      \Drupal::moduleHandler()->alter('cookiebot_culture', $cookiebot_culture);
    }

    $build['cookiebot'][] = [
      '#theme' => 'cookiebot_declaration',
      '#cookiebot_src' => 'https://consent.cookiebot.com/' . $cbid . '/cd.js',
      '#cookiebot_culture' => $cookiebot_culture,
    ];
  }
}
+8 −0
Original line number Diff line number Diff line
@@ -125,6 +125,13 @@ class CookiebotForm extends ConfigFormBase {
      '#default_value' => $config->get('cookiebot_iab_enabled'),
    ];

    $form['cookiebot_drupal_culture'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Use the current Drupal language'),
      '#description' => $this->t("Use the current Drupal language for the cookie popup. If not set Cookiebot will autodetect the language from the user's browser."),
      '#default_value' => $config->get('cookiebot_drupal_culture'),
    ];

    $form['cookiebot_declaration'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Cookie declaration'),
@@ -264,6 +271,7 @@ class CookiebotForm extends ConfigFormBase {
      ->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_drupal_culture', $form_state->getValue('cookiebot_drupal_culture'))
      ->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'))
+21 −2
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ namespace Drupal\cookiebot\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -25,6 +26,13 @@ class CookieDeclarationBlock extends BlockBase implements ContainerFactoryPlugin
   */
  protected $configFactory;

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Constructs a new Block object.
   *
@@ -36,10 +44,13 @@ class CookieDeclarationBlock extends BlockBase implements ContainerFactoryPlugin
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory;
    $this->languageManager = $language_manager;
  }

  /**
@@ -50,7 +61,8 @@ class CookieDeclarationBlock extends BlockBase implements ContainerFactoryPlugin
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('config.factory')
      $container->get('config.factory'),
      $container->get('language_manager')
    );
  }

@@ -71,6 +83,13 @@ class CookieDeclarationBlock extends BlockBase implements ContainerFactoryPlugin
      '#cookiebot_src' => 'https://consent.cookiebot.com/' . $cbid . '/cd.js',
    ];

    $culture = $config->get('cookiebot_drupal_culture');
    if ($culture) {
      $cookiebot_culture = \Drupal::languageManager()->getCurrentLanguage()->getId();
      \Drupal::moduleHandler()->alter('cookiebot_culture', $cookiebot_culture);
      $build['cookie_declaration_block']['#cookiebot_culture'] = $cookiebot_culture;
    }

    return $build;
  }

+2 −1
Original line number Diff line number Diff line
@@ -5,8 +5,9 @@
 *
 * Available variables:
 * - cookiebot_src
 * - cookiebot_culture
 */
#}
{% if cookiebot_src %}
  <script type="text/javascript" id="CookieDeclaration" src="{{ cookiebot_src }}" async="async"></script>
  <script type="text/javascript" id="CookieDeclaration" src="{{ cookiebot_src }}" async="async"{% if cookiebot_culture %} data-culture="{{ cookiebot_culture }}">{% endif %}</script>
{% endif %}