Problem/Motivation
Counter-intuitive behaviour; see issue title.
Steps to reproduce
- Set up some categories, and make at least one "Checked by default".
- 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.
- 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?
Issue fork eu_cookie_compliance-3231985
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:
Comments
Comment #2
svenryen CreditAttribution: svenryen at Ramsalt Lab commentedComment #3
svenryen CreditAttribution: svenryen at Ramsalt Lab commentedTo test this issue:
Comment #6
svenryen CreditAttribution: svenryen at Ramsalt Lab commentedComment #7
Neslee Canil Pinto@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.
Comment #10
Neslee Canil PintoMerged it to 7.x and 8.x branch. Thanks.
Comment #11
Ambient.ImpactAwesome, 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?Comment #12
Neslee Canil Pinto@Ambient.Impact yes, this merge should fix your issue.
Comment #13
svenryen CreditAttribution: svenryen at Ramsalt Lab commentedYes @Ambient.Impact the commit accounts for default categories being checked.
Comment #14
Ambient.ImpactExcellent to hear, thanks!