Klaro Cookie Consent: Storefront Integration
Storefront integration of Klaro Cookie Consent for Sylius 2 - Twig Hooks, Twig functions, magic-anchor trigger and theme compatibility.
Klaro Cookie Consent: Storefront Integration
This documentation always describes the latest published version.
Twig Hooks
The plugin hooks into the standard Sylius shop theme through three Twig Hooks, nothing needs to be wired up manually:
| Hook | Effect |
|---|---|
sylius_shop.base#stylesheets |
Loads klaro.css and the color overrides from the channel configuration; with Google Consent Mode active, also the default denied values in <head> |
sylius_shop.base#javascripts |
Builds window.klaroConfig, loads klaro.js (deferred), wires the Google Consent Mode bridge and the magic-anchor click handler |
sylius_shop.base.footer.content.menu.help_links |
Adds the "Cookie settings" link to the footer service links |
For a channel with no settings row, or with a disabled configuration, none of the three hooks fire: nothing loads and nothing renders.
Theme compatibility
If a custom theme replaces one of the three standard Sylius templates these hooks attach to (base.html.twig, the footer template), the banner, styles or footer link disappear along with it, because the hook point itself is then missing. In that case you need to re-attach the relevant hook point in your own template.
Twig functions
If you want to address the banner or the configuration from your own template:
| Function | Returns | Purpose |
|---|---|---|
mmd_klaro_active(channel) |
bool |
Whether the banner is active for this channel |
mmd_klaro_channel_configuration(channel) |
configuration or null |
The full channel configuration |
mmd_klaro_config(channel) |
array or null |
The finished klaroConfig structure |
mmd_klaro_google_consent_mode_enabled(channel) |
bool |
Whether Google Consent Mode v2 is active for this channel |
mmd_klaro_google_consent_mode_wait_for_update(channel) |
bool |
Whether to wait for the first update |
mmd_klaro_asset_url(file) |
string |
URL of a bundled asset file |
mmd_klaro_plugin_version() |
string |
Version string for cache-busting your own includes |
Magic-anchor trigger
Any link whose href ends in #mmd-klaro-cookie-settings reopens the dialog, whether it lives in a CMS block, a taxon menu entry or a custom template. A single delegated click handler catches the click; no extra JavaScript is needed.
With the built-in footer link left on (
showFooterLink), the same mechanism is also available anywhere else you place it, without a duplicate entry.
window.klaroConfig
The finished configuration is embedded safely via JSON.parse('...'|escape('js')) (no raw HTML-escaping risk), with htmlTexts: false and default: false fixed so admin texts cannot inject HTML and every service starts disabled by default (opt-in).
Headless/API
The storefront integration is currently wired for the Twig theme only. See Troubleshooting: known limitations for details on the missing Shop API support.