lapis / cookie-consent
Cookie consent for Lapis CMS: one accept-or-decline banner, scripts that wait for consent, a list of the cookies in use
Requires
- php: ^8.5
- lapis/cms: ^1.0
Requires (Dev)
- carthage-software/mago: ^1.62
- pestphp/pest: ^5.3
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Cookie consent for Lapis CMS, as simple as it gets: the site lists its outside services once, the banner asks once (accept or decline), and what needs consent waits for it.
composer require lapis/cookie-consent
1. List the services
In the editor, Settings → Cookie consent: each outside service with its purpose, its cookies and a link to its privacy policy (Google Analytics, YouTube, Google Maps, a chat). The banner shows on every page while the list has a service. The banner's text and a link to the privacy page are settings too.
2. Hold back what needs consent
<x-consent> <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"></script> </x-consent> <x-consent placeholder="The video needs cookies from YouTube."> <iframe src="https://www.youtube-nocookie.com/embed/…"></iframe> </x-consent>
The content is a <template> until the visitor accepts, so nothing loads or runs before. With a placeholder, that text and an Accept button show in its place. A page with <x-consent> gets the banner even if the list is empty.
3. The privacy page
<x-cookie-list />
A table of the services and their cookies, and a "Change my choice" button that opens the banner again.
How it works
The choice is one cookie, lapis_consent (1 or 0, for a year), set and read in the browser, with no server call, so pages stay in the page cache. Declining is one click, like accepting. Withdrawing consent reloads the page, since loaded scripts cannot be taken back. Cookies an outside service has already set stay in the browser: they belong to its domain.
Its script is inline: a site with its own Content-Security-Policy needs 'unsafe-inline' in script-src (or a hash of the script). The texts are under cookie_consent.* (English and Hungarian included); a site changes them in its own app/localization.<locale>.yml.
Its look
The banner has the site's font and the browser's default colours. Three ways to change it:
-
Variables, anywhere in the site's CSS,
:roottoo::root { --lapis-consent-background: #fff8ef; --lapis-consent-button-background: #8a4b16; --lapis-consent-button-color: #fff; }
background,color,border,radius,shadow,padding,width,offset(from the window's edges),font,gap,button-background,button-color,button-border,button-radius,button-padding, each as--lapis-consent-…. -
Plain rules. The plugin's styles sit in
@layer lapis-consentbehind:where(), so any rule of the site wins, whatever its specificity:.lapis-consent { inset: auto 0 0; max-width: none; }makes it a full-width bar. The classes:.lapis-consent,.lapis-consent-text,.lapis-consent-buttons,.lapis-consent-button,.lapis-consent-placeholder. -
Replace it. The styles are the component
x-consent-style, the banner's HTMLx-consent-banner. A site's owncomponents/x-consent-style.view.phptakes the plugin's place (an empty file: no styles from the plugin at all), and so does acomponents/x-consent-banner.view.php. A banner of your own keepsid="lapis-consent",hiddenand the two buttons withdata-lapis-consent="1"and"0": the plugin's script drives those.
There are no categories (statistics, marketing) to choose one by one: one choice covers every listed service.