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 behavior was introduced in the patch which first added to ability to convert PHP arrays to JavaScript arrays. It was committed in CVS commit 25401. No rationale was given for the behavior. The attached patch fixes it.
Comment | File | Size | Author |
---|---|---|---|
#18 | drupal_to_js-array-DRUPAL-4.7.patch | 1.2 KB | Darren Oh |
#14 | drupal_to_js-array-DRUPAL-5.patch | 1.2 KB | Darren Oh |
#12 | drupal_to_js-array_1.patch | 1.15 KB | nedjo |
#11 | drupal_to_js-array_0.patch | 1.17 KB | nedjo |
#5 | common.inc-4.7_0.patch | 1.1 KB | Darren Oh |
Comments
Comment #1
Darren OhComment #2
Darren OhHere's a Drupal 4.7 patch for Chat Room users.
Comment #3
nedjoYes, if empty, it's more appropriate that the array be returned as
[]
than{}
.However, we shouldn't break the existing conversion. The current logic is to output arrays when we have sequential integer keys beginning with 0, and otherwise use objects. This is in line with Javascript conventions. Yes, it's possible to use string or non-integer keys for Javascript arrays, but doing so breaks many common Javascript array handling methods and approaches.
So falling through and converting arrays with string or non-sequential numerical keys as objects is appropriate. What was missed is just the empty case.
Attached patch adds a test for an empty array, plus adds some code comments to clarify the logic. This addresses the issue without changing current conversion logic.
Comment #4
Darren OhBeautiful, nedjo! The patch solves the problem. Here's a patch for Drupal 4.7.
Comment #5
Darren OhBeautiful, nedjo! The patch solves the problem. Here's a patch for Drupal 4.7.
Comment #6
Dries CreditAttribution: Dries commentedI'm not Javascript expert yet, but this looks good to me. It would, however, be helpful to explain the code comments _why_ we convert to an array (and _why_ we convert to an object). I don't fully understand why we insist on returning an array ...
Comment #7
Darren OhThese two lines of JavaScript taught me the consequences of this error:
If chatroom.userColours is an empty array, j is zero. If chatroom.userColours is an empty object, j is NaN. When j is NaN, the whole script aborts on the line 2. So the purpose of returning an array is to prevent NaN from being used as an array key.
Comment #8
Crell CreditAttribution: Crell commentedThere's some potentially relevant commentary on the subject of JavaScript notation here: http://killersoft.com/randomstrings/2007/02/28/php-and-json-cut-987/ . (Found courtesy Planet PHP.)
Comment #9
Darren OhI read through the article, but it doesn't seem to be relevant. The main reason to return an array is that it has a length property which some scripts depend on. Do we really need a lengthy explanation of this in the comments?
Comment #10
Dries CreditAttribution: Dries commentedDo you have the snippet handy that shows where the array comes from (the PHP code)? Sorry for getting a little bit of topic, but I really like to grok the bigger issue.
Comment #11
nedjoI'll try to explain better, and fix the comment.
In Drupal, we often use arrays with string keys. In Javascript, associative arrays are considered harmful:
So we apply a test--if it looks like a Javascript array (keys of 0,1,2, etc.), we convert it as an array. If not, we convert it as an object, which is the more appropriate Js equivalent of e.g. a Drupal Forms API array, where we have strings for keys.
This is all working already. The only issue addressed here (besides documentation) is, what to do when the array is empty? The current code says, "I don't find a range of whole number keys starting with 0, so output it as an object". The patch says, if it's empty, that's a valid array, so convert it as an array.
Comment #12
nedjoMore to the point, perhaps, the JSON array notation simply won't accept associative keys--it's not an option. (JSON arrays are constructed as
[value1, value2, ...]
; the keys are assigned automatically.) I've changed the comment to clarify this.Comment #13
Darren OhPHP code snippet for Dries:
I made this to work with my version of the patch by converting the main array to an object. That line would be unnecessary now.
Comment #14
Darren OhA patch for Drupal 5.
Comment #15
skyl1ne CreditAttribution: skyl1ne commentedRE: A patch for Drupal 5
Do I just paste that at the end of the common.inc file?
Thank you.
Comment #16
Darren OhNo. The patch shows which lines to change in the drupal_to_js function. Instructions for applying patches automatically are at http://drupal.org/node/60108.
Comment #17
Dries CreditAttribution: Dries commentedI've committed this change to CVS HEAD and DRUPAL-5. Thanks for the careful explanation, I got it now. Rock.
Comment #18
Darren OhPatch for Drupal 4.7.
Comment #19
skyl1ne CreditAttribution: skyl1ne commentedI was able to install the path for Drupal 5.1 without any problem, but am still unable to submit text or have it show up in the chat window. Any help would be appreciated.
Thank you.
Comment #20
Crell CreditAttribution: Crell commentedPlease do not change the title. Thanks.
Comment #21
kkaefer CreditAttribution: kkaefer commented(This is a duplicate of http://drupal.org/node/78766, closing the other one since this issue has patches committed)
Comment #22
lachralle CreditAttribution: lachralle commentedPlease add me to the list of ppl who have applied the change to common.inc in drupal 5.1 but still can't send chat messages in both MacOpera and Camino.
Comment #23
nedjoPlease move comments about a bug in chat module to an issue on that module.
Comment #24
Darren OhThe correct issue for Ralf's Chat room bug is 126181.
Comment #25
kkaefer CreditAttribution: kkaefer commentedFor a way to force non-numeric arrays to convert to arrays instead of objects (which will result in a renumbering of course), see http://drupal.org/node/126548.
Comment #26
sean2000 CreditAttribution: sean2000 commentedI have a similar problem as skyl1ne. I applied the patch (I'm using drupal 5.1), but it still isn't helping. When I type a message and push "Send", nothing happens and the messages aren't being written to the tables either.
Please contact me if you have a solution. Thanks!
Comment #27
Darren OhPlease stick to the subject. There is already a bug report open for Chat room.
Comment #28
killes@www.drop.org CreditAttribution: killes@www.drop.org commentedapplied
Comment #29
gurukripa CreditAttribution: gurukripa commentedhi guys,
i want to use the chat module and i am not a wizkid like most of u..
i noticed that there is a patch..and i am using 5.1
can someone kindly certify whether the module is ready for use...or needs the patch...
my request is that someone kindly fix the module and make a release so we wont have to make a patch, since newbies like me feel nervous with this kinda thing.
look frd to someone helping me out here...tell me what to do.
thanks...
Comment #30
nedjoThird time now: if you have an issue with chatroom module, look for an issue there, or open a new one:
http://drupal.org/project/issues/chatroom
Please don't reopen this issue again.
Comment #31
(not verified) CreditAttribution: commented