Problem/Motivation

Counter-intuitive behaviour; see issue title.

Steps to reproduce

  1. Set up some categories, and make at least one "Checked by default".
  2. Go to a page where the pop-up can be opened, and open it. Checked by default category will be checked, as expected without any choice yet made. Uncheck that category and save preferences.
  3. Open the pop-up again. Notice that the checked by default category is now checked again, even though you unchecked it.

Proposed resolution

After some digging, I noticed that the HTML for the pop-up starts out with the checked attribute applied to the category checkboxes that are to be checked by default, which makes sense. Then I looked through the JavaScript code and noticed that Drupal.eu_cookie_compliance.setPreferenceCheckboxes() only sets the checked DOM property to true on the checkboxes for the categories the user has agreed to, but doesn't touch the ones that weren't agreed to. I'm not familiar with the JavaScript for this, but I'm guessing it's doing something that replaces the DOM tree with the default markup, thus likely obliterating the state of the checkboxes each time the pop-up opens, in which case browsers naturally fall back to using the checked HTML attribute to determine the initial

Remaining tasks

This is the current method:

  Drupal.eu_cookie_compliance.setPreferenceCheckboxes = function (categories) {
    for (var i in categories) {
      $("#eu-cookie-compliance-categories input:checkbox[value='" + categories[i] + "']").prop("checked", true);
    }
  }

The problem is that categories only contains the agreed to categories. This seems to fix it:

  Drupal.eu_cookie_compliance.setPreferenceCheckboxes = function (categories) {
    // Set all checkboxes to unchecked before applying existing categories so
    // checked by default categories that weren't agreed to correctly display
    // their saved state.
    $("#eu-cookie-compliance-categories input:checkbox").prop("checked", false);

    for (var i in categories) {
      $("#eu-cookie-compliance-categories input:checkbox[value='" + categories[i] + "']").prop("checked", true);
    }
  }

User interface changes

None?

API changes

Probably none?

Data model changes

None?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Ambient.Impact created an issue. See original summary.

svenryen’s picture

Assigned: Unassigned » svenryen
svenryen’s picture

To test this issue:

  • Set your method to "Opt in with Categories"
  • Create at least one of each kind of category (Unchecked, Checked, Disabled)
  • Clear cookies
  • Toggle on and off the various options and save every time, confirming that we always have the same checkboxes CHECKED after saving and reopening the banner

svenryen’s picture

Status: Active » Needs review
Neslee Canil Pinto’s picture

Version: 8.x-1.14 » 8.x-1.x-dev
Status: Needs review » Reviewed & tested by the community

@svenryen tested the merge request for both drupal 7 - https://mr29-8hgptcpuwie94u5wyj81uxpja6cfzcwu.tugboat.qa and drupal 8 - https://mr30-olsu0riwbdsyhxlmh8rnyvszhg0nswvv.tugboat.qa and the js code looks good.

Neslee Canil Pinto’s picture

Assigned: Neslee Canil Pinto » Unassigned
Status: Reviewed & tested by the community » Fixed

Merged it to 7.x and 8.x branch. Thanks.

Ambient.Impact’s picture

Awesome, thanks for the work! I discovered a problem with the method I provided to fix the issue that I hadn't considered: Drupal.eu_cookie_compliance.setPreferenceCheckboxes() ends up unchecking all categories, including checked by default categories, so those never end up being checked by default. Does the fix account for this?

Neslee Canil Pinto’s picture

@Ambient.Impact yes, this merge should fix your issue.

svenryen’s picture

Yes @Ambient.Impact the commit accounts for default categories being checked.

Ambient.Impact’s picture

Excellent to hear, thanks!

  • Neslee Canil Pinto committed aa6fb83 on 7.x-2.x authored by svenryen
    Issue #3231985 by svenryen, Neslee Canil Pinto, Ambient.Impact: "Checked...
  • svenryen committed baf741b on 7.x-2.x
    Merge branch '7.x-1.x' into 7.x-2.x
    
    * 7.x-1.x:
      Issue #3236586 by...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.