You are here

cookies_matomo.module in COOKiES Consent Management 1.0.x

File

modules/cookies_matomo/cookies_matomo.module
View source
<?php

/**
 * @file
 * Contains cookies_matomo.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Drupal\cookies\CookiesKnockOutService;

/**
 * Implements hook_help().
 */
function cookies_matomo_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the cookies_matomo module.
    case 'help.page.cookies_matomo':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Submodule of cookies to manage Matomo Analytics implemented by so named module.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_page_attachments().
 */
function cookies_matomo_page_attachments(&$page) {
  $doKo = CookiesKnockOutService::getInstance()
    ->doKnockOut();
  if ($doKo) {
    foreach ($page["#attached"]["html_head"] as $key => $head_tag) {
      if (in_array('matomo_tracking_script', $head_tag)) {
        $attr = isset($head_tag[0]['#attributes']) ? $head_tag[0]['#attributes'] : [];
        $attr = array_merge($attr, [
          'type' => 'application/json',
          'id' => 'cookies_matomo',
        ]);
        $page["#attached"]["html_head"][$key][0]['#attributes'] = new Attribute($attr);
        $page["#attached"]["library"][] = 'cookies_matomo/analytics';
        break;
      }
    }
  }
}

Functions