Search by

protonsystems / cookie-consent

proton.systems

Reusable, theme-overridable Drupal 11 cookie consent module built on vanilla-cookieconsent, used headlessly.

Package info

gitlab.com/Proton.Systems/drupal/cookie-consent

Issues

Type:drupal-module

pkg:composer/protonsystems/cookie-consent

Statistics

Installs: 22

Dependents: 0

Suggesters: 0

Stars: 0

v1.1.0 2026-10-07 18:34 UTC

This package is auto-updated.

Last update: 2026-10-07 23:35:01 UTC


README

Reusable, theme-overridable Drupal 11 module for cookie consent — a first-visit banner, a preferences dialog, a persistent reopen badge, and consent-gated iframes/scripts, built on vanilla-cookieconsent 3.1.0 used headlessly (as a consent-state engine only; all rendered UI is Drupal's own Twig/CSS).

Purpose

The module is intended to be reused across Drupal 11 projects by installing it as a Composer package and enabling it in the target site.

Requirements

  • Drupal 11
  • PHP 8.3+

Features

  • Renders a first-visit banner, a native <dialog> preferences modal, and a persistent "reopen settings" badge on every non-admin page automatically
  • Three consent categories: required (always on), marketing, external
  • Gates third-party <iframe> embeds behind consent, with a same-origin/ allowlisted-hosts check before ever loading one
  • Gates <script> tags that draw visible UI, showing a placeholder until their category is accepted
  • All copy, categories, cookie settings, embed allowlist, and policy links are editable via config — no template/PHP changes needed per project
  • Every piece of UI is its own theme hook, independently overridable per-theme
  • Colors and typography are public --cookie-consent-* CSS variables, overridable from theme CSS or the admin form — no need to replace the stylesheet for a rebrand

Structure

  • cookie_consent.module contains the theme, page, and preprocess hooks.
  • src/Form/ contains the admin settings form.
  • src/CssVariables.php defines the admin-overridable CSS variables and validates/emits them.
  • templates/ contains the banner/modal/badge/gate/embed Twig templates.
  • css/ and js/ contain the default styling and the ported consent behavior, plus the vendored vanilla-cookieconsent library.
  • config/ contains shipped defaults and config schema.
  • docs/module-architecture.md documents how the module is built — the gating mechanism, theme hook contract, and override points — for anyone changing or extending it.

Installation

composer require protonsystems/cookie-consent

Then enable the module in Drupal:

drush en cookie_consent

Configure categories, policy links, the embed allowlist, and all UI text at /admin/config/system/cookie-consent (permission: administer cookie consent), then drush cex to keep that configuration in config/sync.

Theme Integration

No theme changes are required to get a working, styled UI — the module ships its own default CSS and templates render automatically on every non-admin page.

Restyling with CSS variables

For a rebrand (colors, fonts) you don't need to touch templates or disable the stylesheet. Every variable prefixed --cookie-consent- is public API. Override them from your theme's CSS:

:root {
  --cookie-consent-accent: #c2185b;
  --cookie-consent-accent-fg: #fff;
  --cookie-consent-font-family: "Inter", sans-serif;
}

Or, without a theme change, at Appearance on /admin/config/system/cookie-consent (values are validated, then emitted as a :root rule in <head>; empty fields keep the default). If both set the same variable, theme CSS wins, then the admin form, then the module default.

VariableDefaultUsed for
--cookie-consent-bg#fffBanner, modal card, badge, buttons
--cookie-consent-fg#1a1a1aText
--cookie-consent-muted#5a5a5aSecondary text
--cookie-consent-border#d8d8d8Borders, dividers
--cookie-consent-accent#0f62fePrimary button, checkboxes, badge icon
--cookie-consent-accent-fg#fffText on the accent color
--cookie-consent-accent-hoveraccent darkened 15%Primary button hover
--cookie-consent-button-hover-bgfg mixed 8% into bgSecondary button hover
--cookie-consent-focus-ringaccent:focus-visible outline
--cookie-consent-embed-bgborder at 25%Embed / script-gate placeholder
--cookie-consent-backdroprgba(0, 0, 0, 0.5)Behind the modal
--cookie-consent-attribution-fgrgba(255, 255, 255, 0.85)Attribution line (sits on the backdrop)
--cookie-consent-font-familyinheritAll components
--cookie-consent-font-sizeinheritBase size
--cookie-consent-font-size-small0.9emDescriptions, links, badge label
--cookie-consent-font-size-xsmall0.8emEmbed note, attribution
--cookie-consent-line-heightinheritAll components
--cookie-consent-font-weight-strong600Category and embed titles
--cookie-consent-radius8pxCards, buttons, placeholders
--cookie-consent-badge-radius3remReopen badge
--cookie-consent-shadow0 2px 16px rgba(0, 0, 0, 0.15)Banner, modal, badge
--cookie-consent-z99998Stacking (badge is one lower)

The last four (radius, badge radius, shadow, z-index) are themeable but not in the admin form.

Keep the pairings readable — the module does not check contrast for you. Aim for WCAG AA: fg/muted on bg and accent-fg on accent at 4.5:1; border and focus-ring against bg at 3:1; attribution-fg against the backdrop at 4.5:1. If you lighten the backdrop, set attribution-fg too — its default is white. If your CSP forbids inline styles, use theme CSS rather than the admin form.

Replacing the markup or the stylesheet

To restyle a piece of UI beyond what variables allow, copy its template into the theme's templates/ directory (Drupal's standard template discovery picks it up) and disable the module's default CSS via libraries-override in the theme's .info.yml.

To gate a specific embed server-side (e.g. a video field), render #theme => 'cookie_consent_embed' in place of the real iframe from a project-specific hook_preprocess_HOOK().

See docs/module-architecture.md for the full theme hook reference, configuration reference, and customization details.

Notes

The module currently uses Drupal core services directly and is designed to be consumed as a standard Drupal module package.