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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kscheirer’s picture

patch 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:

  1. Clean install, apply patch
  2. login to your site. at this point you can verify that has_js = 1 when javascript is enabled in your browser by inspecting cookies via your browser manually (preferences / privacy / show cookies in firefox)
  3. go to admin/user/permissions and allow anonymous users to comment w/o approval
  4. create a node of any type, 'page' is good
  5. go to admin/build/node-type/page, under comment settings and anonymous commenting, select Anonymous posters may leave their contact information
  6. logout
  7. go to your created node and click 'add new comment'
  8. verify your contact information in the submitted comment
  9. now comment or reply again, and the name, mail, homepage fields should be filled out with your previous info (they should also be available through your browser, see step 2)
kkaefer’s picture

The 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.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Code looks useful and clean. Love the clean new API ... Session tests all pass. RTBC.

mfer’s picture

Status: Reviewed & tested by the community » Needs work

In Drupal.behaviors.comment both jQuery and $ are used to call jQuery. This is inconsistent. We should use $ in both places.

BrightLoudNoise’s picture

Patch no longer applies cleanly, can re-roll if there is still interest.

moshe weitzman’s picture

yes please

BrightLoudNoise’s picture

Status: Needs work » Needs review
FileSize
5.11 KB

Patch re-rolled against HEAD

It is saving the cookies, but not does not retrieve them as indicated in step 9. Any ideas?

RobLoach’s picture

What 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.

Status: Needs review » Needs work

The last submitted patch failed testing.

markus_petrux’s picture

If 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.

kkaefer’s picture

What 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.

markus_petrux’s picture

I 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.

+      $cookie_params = session_get_cookie_params();
       $javascript = array(
         'settings' => array(
           'data' => array(
             array('basePath' => base_path()),
+            array('cookiePath' => $cookie_params['path']),
+            array('cookieDomain' => $cookie_params['domain']),
+            array('cookieSecure' => $cookie_params['secure']),
           ),
           'type' => 'setting',
           'scope' => 'header',
           'weight' => JS_LIBRARY,
         ),
kkaefer’s picture

We'd then have to transmit that data on every page, which seems a bit wasteful to me.

markus_petrux’s picture

Well, 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.

markus_petrux’s picture

hmm... 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.

kkaefer’s picture

Status: Needs work » Needs review
FileSize
5 KB

We 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 (both HEAD and DRUPAL-6--1) and found only very few modules using has_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 on jQuery.cookie() working as expected. It also fixes a small problem with the comment cookies where space showed up as + when the values were retrieved.

markus_petrux’s picture

Status: Needs review » Needs work

basePath 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.

yched’s picture

"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

markus_petrux’s picture

My 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. :)

mfer’s picture

Why 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?

yched’s picture

I 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.

kkaefer’s picture

(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 ?

yched’s picture

Probably 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 ? ;-)

kkaefer’s picture

Sorry, it got filtered out. I asked whether we could use <noscript> instead of the has_js cookie.

RobLoach’s picture

Why are we deviating off of http://plugins.jquery.com/files/jquery.cookie.js.txt ?

The function `$.cookie()` is based on and API compatible with............

.... Ah, to use Drupal.settings.basePath?

moshe weitzman’s picture

@robloach - that was asked in the first followup and answered in the second. ?

Damien Tournoud’s picture

#507072: Add jquery cookie library introduced the vanilla jQuery Cookie library into core. Let's refocus that issue on using it ;)

moshe weitzman’s picture

There are still some improvements from the most recent patch here that should get into d7. anyone willing to reroll using our new cookie library?

Dave Reid’s picture

Status: Needs work » Fixed

Actually they were all fixed independently.

Status: Fixed » Closed (fixed)

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