Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This patch adds a function to drupal.js that does cookie handling (adding, deleting and retrieving). The patch also converts the existing cases in Drupal's JS where we use cookies to the new function to remove redundant implementations.
Comment | File | Size | Author |
---|---|---|---|
#16 | jquery-cookie-1.patch | 5 KB | kkaefer |
#7 | js-cookie_02.patch | 5.11 KB | BrightLoudNoise |
js-cookie.patch | 5.08 KB | kkaefer | |
Comments
Comment #1
kscheirerpatch applied cleanly and works for me, passed simpletests.
Is adding cookie functions to the Drupal object preferable to using a jQuery cookie plugin?
Drupal isn't doing a lot of cookie-ing, just the has_js and when anonymous users comment with contact info.
To test:
Anonymous posters may leave their contact information
Comment #2
kkaefer CreditAttribution: kkaefer commentedThe new function is 691 bytes of code (not counting comments) and the function that is removed (that does only retrieval of cookies!) is about 400 bytes (minified). Additionally, the code for the comment cookie handing is more concise; so this patch doesn't really add a lot of JS code to Drupal. Thank you for the instructions for how to test this patch.
Comment #3
moshe weitzman CreditAttribution: moshe weitzman commentedCode looks useful and clean. Love the clean new API ... Session tests all pass. RTBC.
Comment #4
mfer CreditAttribution: mfer commentedIn Drupal.behaviors.comment both jQuery and $ are used to call jQuery. This is inconsistent. We should use $ in both places.
Comment #5
BrightLoudNoise CreditAttribution: BrightLoudNoise commentedPatch no longer applies cleanly, can re-roll if there is still interest.
Comment #6
moshe weitzman CreditAttribution: moshe weitzman commentedyes please
Comment #7
BrightLoudNoise CreditAttribution: BrightLoudNoise commentedPatch re-rolled against HEAD
It is saving the cookies, but not does not retrieve them as indicated in step 9. Any ideas?
Comment #8
RobLoachWhat if we were to bring in the jQuery Cookie plugin instead? Open source is all about working together on stuff, and the plugin is both GPL and MIT, so I don't see why not..... Unsubscribing.
Comment #10
markus_petrux CreditAttribution: markus_petrux commentedIf path, domain and secure options where sent from the server in Drupal.settings, then Drupal.cookie could use those options to provide defaults. And that would cover the client-side aspect of another issue I opened yesterday: #445098: Provide a consistent method to generate cookies.
Using path and domain passed by Drupal.settings would generte cookies under the same scope Drupal session cookies are generated. Also, callers of Drupal.cookie would not have to think about the secure option.
Comment #11
kkaefer CreditAttribution: kkaefer commentedWhat we could do is to use a modified version of the jQuery plugin (which seems unmaintained, btw) that uses our Drupal.settings.basePath as default path.
Comment #12
markus_petrux CreditAttribution: markus_petrux commentedI believe basePath does not necessarily need to match cookie path setting. For example, basePath could be '/mysite/', but cookie path be set to '/'.
Instead, drupal_add_js() could populate cookiePath, cookieDomain and cookieSecure to provide defaults in Drupal.cookie consistent with session cookies.
Comment #13
kkaefer CreditAttribution: kkaefer commentedWe'd then have to transmit that data on every page, which seems a bit wasteful to me.
Comment #14
markus_petrux CreditAttribution: markus_petrux commentedWell, then there could be a server-side function that adds cookie stuff to client-side. Something like drupal_add_js_cookies(), and there's where these options are populated to Drupal.settings, and the javascript methods added to the Drupal object.
Comment #15
markus_petrux CreditAttribution: markus_petrux commentedhmm... but we need "has_js" on every page, so my previous idea does not worth.
Anyway, we already send basePath minimum on every page that has javascript. The cookie stuff is not so much data, it's just a path, a domain and a boolean. :-/
PS: Sorry for double posting.
Comment #16
kkaefer CreditAttribution: kkaefer commentedWe should kill the
has_js
cookie. It’s only used for batch API in core and I’m sure there is a way how we can make batch API work without that cookie. I grepped contrib (bothHEAD
andDRUPAL-6--1
) and found only very few modules usinghas_js
(and I’m sure these can be made work without the cookie as well).Attached is a patch that is based on
jQuery.cookie()
, but with many cleanups from the previous patch. The API is compatible, though, so third-party scripts can rely onjQuery.cookie()
working as expected. It also fixes a small problem with the comment cookies where space showed up as+
when the values were retrieved.Comment #17
markus_petrux CreditAttribution: markus_petrux commentedbasePath doesn't necessary need to match cookie path. Also, still not passing session_get_cookie_params() to Drupal.settings.
If we get rid of "has_js" cookie, then I think we could provide drupal_add_js_cookies() API that would expose the javascript API for cookies, and cookie settings in Drupal.settings. Also, creation of "has_js" would have to be moved to batch API.
Comment #18
yched CreditAttribution: yched commented"Also, creation of "has_js" would have to be moved to batch API.".
Batch API relies on the fact that has_js has been set by some (non batch-related) drupal_add_js() in one of the previous pages in the browsing session.
If batch API becomes the only setter of has_js, then you lose the feature, and all/most batch processing pages will be served using the non-JS refresh
Comment #19
markus_petrux CreditAttribution: markus_petrux commentedMy comment was just a response to #16. In the same line: If Batch API needs someone else to set the "has_js" cookie, then Batch API could provide an API to do so.
On the other hand, if "has_js" is part of the Drupal framework by itself, then it doesn't make sense to remove it. :) ...but then, I would say it needs to be created under the same scope as session cookies, and this would require to append cookie options to Drupal.settings for all pages that output javascript. This would provide a consistent method to define cookies that can be shared between subdomains on the same installation.
Worths the cost of sending a path, a domain and a boolean in Drupal.settings for this? IMHO, yes. :)
Comment #20
mfer CreditAttribution: mfer commentedWhy are we loading the cookie js on every page? Wouldn't it be more appropriate to keep it as an eternal library and call it when it's needed? Or, am I missing something?
Comment #21
yched CreditAttribution: yched commentedI definitely didn't follow the ins and outs of the issue here. Just to bring more light on has_js / batch API :
The Batch API JS behavior is one that cannot degrade within the same HTML. If JS is disabled, you need to serve different HTML, so you need to get the 'JS enabled' info from a previous page request.
The intent of using the existing has_js cookie was to make the use of batch API more accessible, and avoid requiring specific code on the page that potentially triggers the batch processing. has_js solution is not perfect, but limits the chances of false positive (which would be critical - the only case where this breaks is if you intentionally disable JS just before triggering the batch)
Other suggestions for this are definitely welcome, but not having to 'prepare' for batch processing from a previous page is fairly precious IMO.
Comment #22
kkaefer CreditAttribution: kkaefer commented(As a side note, if you disable JS but have the cookie already set, batch API will fail)
Is there a reason why we can't use ?
Comment #23
yched CreditAttribution: yched commentedProbably a crosspost ?
"(As a side note, if you disable JS but have the cookie already set, batch API will fail)"
Yes - from my #21: "the only case where this breaks is if you intentionally disable JS just before triggering the batch". We left this (really edge) case out on purpose, for lack of a better proposal. Again, the current is not perfect, but the pros outweight the cons.
"Is there a reason why we can't use ?"
Missing bit ? ;-)
Comment #24
kkaefer CreditAttribution: kkaefer commentedSorry, it got filtered out. I asked whether we could use
<noscript>
instead of the has_js cookie.Comment #25
RobLoachWhy are we deviating off of http://plugins.jquery.com/files/jquery.cookie.js.txt ?
.... Ah, to use Drupal.settings.basePath?
Comment #26
moshe weitzman CreditAttribution: moshe weitzman commented@robloach - that was asked in the first followup and answered in the second. ?
Comment #27
Damien Tournoud CreditAttribution: Damien Tournoud commented#507072: Add jquery cookie library introduced the vanilla jQuery Cookie library into core. Let's refocus that issue on using it ;)
Comment #28
moshe weitzman CreditAttribution: moshe weitzman commentedThere are still some improvements from the most recent patch here that should get into d7. anyone willing to reroll using our new cookie library?
Comment #29
Dave ReidActually they were all fixed independently.