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,
Comment | File | Size | Author |
---|---|---|---|
#5 | user-cookie-save-signature.patch | 3.73 KB | jpmckinney |
user-cookie-save-signature.patch | 4.08 KB | jpmckinney | |
Comments
Comment #1
jpmckinney CreditAttribution: jpmckinney commentedTypos: If we don't like
Typos:
Admittedly,Comment #2
Dries CreditAttribution: Dries commentedThat looks like a nice little clean-up. Given that user_cookie_save() is brand new, that seems like an OK change to make.
Comment #3
andypostPrevious signature looks more readable then
array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage')))
maybe better incorporate this into function itself?
code-style
Comment #4
Dries CreditAttribution: Dries commentedI 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.
Comment #5
jpmckinney CreditAttribution: jpmckinney commentedFix 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.
Comment #6
Dries CreditAttribution: Dries commentedRTBC for me.
Comment #7
Dries CreditAttribution: Dries commentedAlright, committed to CVS HEAD.