PHP template conditions?

Created on 9 August 2023, over 1 year ago
Updated 15 October 2023, about 1 year ago

Motivation

I'm trying to find a way to setup conditions in my page template to not show portions based on the consent cookie value.
Wondering if there might any sort of PHP if statement that I can use similar to the Javascript of if (Drupal.eu_cookie_compliance.hasAgreed()){ }. Or possibly calling the Consent Cookie value as a PHP variable $_COOKIE['cookiename'] and then use an if statement surrounding my template portion.

I do see I can use Basic Storage of the consent choice, which made me wonder if there might be a PHP condition when using that storage.
Which I realize does go against the whole consent to cookies option.

This isn't an issue really, just was hoping to get some help.
I am working with both Drupal 7 and Drupal 9 sites to implement this.

Thanks for any help you can provide.

πŸ’¬ Support request
Status

Active

Version

1.43

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jberg1

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jberg1
  • πŸ‡³πŸ‡΄Norway svenryen

    Hi!

    I'll try to help, though normally a JavaScript approach would be best.

    Do you serve a Drupal site without any caching layer?

  • πŸ‡ΊπŸ‡ΈUnited States jberg1

    Thanks for your reply Svenryen.

    Currently working with Drupal 7 website, here is what I managed to do.
    First, I installed the Cookie-Aware Page Cache β†’ module that allowed me to remove that cookie from cache. Cookie name specified with EU Cookie Compliance (GDPR Compliance).

    Then using this PHP to check the cookie value in my PHP I'm doing this.

    if(isset($_COOKIE["cookiename"]) && $_COOKIE["cookiename"] == "2" ) {
    do something...
    }

    This allowed me to only present template parts if they have agreed to accept the cookies. Mainly used to show/hide blocks I have for Tracking codes.

    I'll be figuring out a way to do this with Drupal 9 (and 10). But unclear on how to remove a specific cookie from the Cache in that Drupal verison. Since that Cookie-Aware Page Cache is only available for Drupal 7.

    Any advise is welcome.
    Thanks again.

  • πŸ‡³πŸ‡΄Norway svenryen

    I don't really know, and it's a bit beyond what we can offer support for free of charge. I just know cache is usually an issue and many sites prefer to use a javascript based approach where cookie aware information is injected through ajax. That should also work in 9 and 10, and the request to the cookie related information can be cached too.

    If you're interested, I work for an agency that can analyse your needs and offer paid support and likely solve your issue. Please contact us at ramsalt.com if that's of interest.

Production build 0.71.5 2024