Having read #440876: Reuse comment.module's anonymous cookie information, user_cookie_save()'s function signature only makes sense if it is passed form values. In that specific case, you rarely want to save all the form values to a cookie, so it makes sense to pass a second array listing just those keys you want to save.

The problem is, user_cookie_save() is not only being used to save form values to a cookie, and in my opinion the more popular use case for the function in contrib will not be to save form values to a cookie. It is in that case that the function signature is redundant and unwieldy.

Dries touched on this in #748976: "Show/hide descriptions" links no longer work and, when writing the patch for that issue I had a bit of a "*sigh* Drupal" moment when parsing user_cookie_save()'s signature.

This patch changes user_cookie_save()'s signature and updates all calls to it in core.

If we don't link the array_intersect_key pattern, we can have a function user_cookie_save_form_values (or some better name) that wraps user_cookie save and does the array_intersect_key in one place.

Admittedly,

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jpmckinney’s picture

Status: Active » Needs review

Typos: If we don't like
Typos: Admittedly,

Dries’s picture

That looks like a nice little clean-up. Given that user_cookie_save() is brand new, that seems like an OK change to make.

andypost’s picture

Previous signature looks more readable then
array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage')))
maybe better incorporate this into function itself?

+  setrawcookie('Drupal.visitor.'. $cookie_name, '', REQUEST_TIME - 3600, '/');

code-style

Dries’s picture

I don't think array_intersect_key() is hard to understand, and it still makes for a cleaner signature IMO. I'd keep it as proposed in the initial patch.

jpmckinney’s picture

Fix code-style. Re: #3: The signature is more readable than before. The way in which I am currently preparing its arguments is less readable than before. To address that, I proposed having a second function that wraps user_cookie_save() and hides the preparation of these arguments. That wrapper would only be called if the developer wants to store some but not all form values in the cookie. Otherwise, the developer would call user_cookie_save directly.

Dries’s picture

Status: Needs review » Reviewed & tested by the community

RTBC for me.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Alright, committed to CVS HEAD.

Status: Fixed » Closed (fixed)

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