Problem/Motivation
We use a custom module that is pretty similar to what the tarte_au_citron module does (only it is based on the tacjs module). It detects which modules are installed and enables these services in tarteaucitron.js automatically.
Our module supports some modules that tarte_au_citron does not :
- google_analytics
- matomo
- fb_likebox
- facebook_pixel
If we could add support for these in tarte_au_citron, we could use it instead of our custom module.
Proposed resolution
I don't have time to submit patches right now, but I can share the logic we use to detect if the module is enabled and to prevent it from loading its scripts directly.
It would of course need to be adapted for tarte_au_citron, but I think most of the conditions (to see if the module is enabled or to detect the script in attachments) could be reused.
google_analytics
$cache = CacheableMetadata::createFromRenderArray($attachments);
$gaSettings = Drupal::config('google_analytics.settings');
$cache->addCacheableDependency($gaSettings);
if ($ua = $gaSettings->get('account')) {
$services[] = 'gtag';
$variables['gtagUa'] = $ua;
if (isset($attachments['#attached']['html_head'])) {
foreach ($attachments['#attached']['html_head'] as $i => $attachment) {
if (in_array($attachment[1], [
'google_analytics_tracking_file',
'google_analytics_tracking_script',
])) {
unset($attachments['#attached']['html_head'][$i]);
}
}
}
}
$cache->applyTo($attachments);
facebook_pixel
$cache = CacheableMetadata::createFromRenderArray($attachments);
if (Drupal::service('update.update_hook_registry')
->getInstalledVersion('facebook_pixel') >= 8101) {
$settings = Drupal::config('facebook_pixel.settings');
}
else {
$settings = Drupal::config('facebook_pixel.facebookpixelconfig');
}
$cache->addCacheableDependency($settings);
if ($id = $settings->get('facebook_id')) {
$services[] = 'facebookpixel';
$variables['facebookpixelId'] = $id;
if (isset($attachments['#attached']['html_head'])) {
foreach ($attachments['#attached']['html_head'] as $i => $attachment) {
if ($attachment[1] == 'facebook_tracking_pixel_script') {
unset($attachments['#attached']['html_head'][$i]);
}
}
}
if (isset($attachments['#attached']['library'])) {
foreach ($attachments['#attached']['library'] as $i => $library) {
if ($library == 'facebook_pixel/facebook_pixel') {
unset($attachments['#attached']['library'][$i]);
}
}
}
}
$cache->applyTo($attachments);
matomo
$cache = CacheableMetadata::createFromRenderArray($attachments);
$matomoSettings = Drupal::config('matomo.settings');
if ($matomoSettings->isNew()) {
$matomoSettings = Drupal::config('piwik.settings');
}
$cache->addCacheableDependency($matomoSettings);
if ($site_id = $matomoSettings->get('site_id')) {
if ($matomo_url = $matomoSettings->get('url_http')) {
if ($matomo_url_https = $matomoSettings->get('url_https')) {
$matomo_url = $matomo_url_https;
}
$services[] = 'matomohightrack';
$variables['matomoId'] = $site_id;
$variables['matomoHost'] = $matomo_url;
if (isset($attachments['#attached']['html_head'])) {
foreach ($attachments['#attached']['html_head'] as $i => $attachment) {
if ($attachment[1] == 'matomo_tracking_script') {
unset($attachments['#attached']['html_head'][$i]);
}
}
}
}
}
$cache->applyTo($attachments);
fb_likebox
if (\Drupal::entityQuery('block')
->condition('plugin', 'fb_likebox_block')
->accessCheck()
->count()
->execute() > 0) {
$services[] = 'facebooklikebox';
}
With a preprocess to alter the block:
/**
* Implements hook_preprocess_HOOK().
*
* @phpstan-param array<string, mixed> $variables
*/
function insite_tarteaucitron_preprocess_block(&$variables): void {
if ($variables['plugin_id'] == 'fb_likebox_block') {
unset($variables['content']['#attached']['library']);
}
}