You are here

function cookies_gtag_page_attachments in COOKiES Consent Management 1.0.x

Implements hook_page_attachments().

File

modules/cookies_gtag/cookies_gtag.module, line 31
Contains cookies_ga.module.

Code

function cookies_gtag_page_attachments(&$page) {
  $doKo = CookiesKnockOutService::getInstance()
    ->doKnockOut();
  if ($doKo && isset($page["#attached"]["html_head"])) {
    foreach ($page["#attached"]["html_head"] as &$head_tag) {
      if (preg_match('/^google_tag_script_tag/', $head_tag[1])) {
        if (isset($head_tag[0]['#attributes'])) {
          if ($head_tag[0]['#attributes'] instanceof Attribute) {
            $head_tag[0]['#attributes']
              ->setAttibute('id', [
              'cookies_gtag',
            ]);
            $head_tag[0]['#attributes']
              ->setAttibute('type', [
              'application/json',
            ]);
          }
          elseif (is_array($head_tag[0]['#attributes'])) {
            $head_tag[0]['#attributes']['id'] = 'cookies_gtag';
            $head_tag[0]['#attributes']['type'] = 'application/json';
          }
        }
        else {
          $head_tag[0]['#attributes'] = [
            'id' => 'cookies_gtag',
            'type' => 'application/json',
          ];
        }
        $page["#attached"]["library"][] = 'cookies_gtag/gtag';
        break;
      }
    }
  }
}