You are here

cookies_facebook_pixel.module in COOKiES Consent Management 1.0.x

File

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

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

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

    // Main module help for the cookies_facebook_pixel module.
    case 'help.page.cookies_facebook_pixel':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Submodule of COOKiES to manage Facebook Pixel implemented by facebook_pixel module.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_page_attachments().
 */
function cookies_facebook_pixel_page_attachments(&$page) {
  $doKo = CookiesKnockOutService::getInstance()
    ->doKnockOut();

  // Knock-Out supporting the module "Facebook Pixel (facebook_pixel)".
  if ($doKo && isset($page["#attached"]["html_head"])) {
    foreach ($page["#attached"]["html_head"] as $key => $head_tag) {
      $scripts = [
        'facebook_tracking_pixel_script',
      ];
      foreach ($scripts as $script) {
        if (in_array($script, $head_tag)) {
          $attr = isset($head_tag[0]['#attributes']) ? $head_tag[0]['#attributes'] : [];
          $attr = array_merge($attr, [
            'type' => 'application/json',
            'id' => "cookies_{$script}",
          ]);
          $page["#attached"]["html_head"][$key][0]['#attributes'] = new Attribute($attr);
          $page["#attached"]["library"][] = 'cookies_facebook_pixel/facebook_pixel';
          break;
        }
      }
    }
  }
}