In this code block, theme_status_messages() hardcodes a 0 index for the $messages array when theming a single value message array:
if (count($messages) > 1) {
$output .= " <ul>\n";
foreach ($messages as $message) {
$output .= ' <li>' . $message . "</li>\n";
}
$output .= " </ul>\n";
}
else {
$output .= $messages[0];
}
The problem is that if you're doing any sort of manual interaction with the messages session variable, you may end up with a single value array that does not have a 0 key. Instead of adding $messages[0] to $output, this could be changed to reset($messages) if desired. I'm sure there are other ways to deal with this, the most robust of which will not get into 7.x - provide a proper API around status messages. ^_^
Comment | File | Size | Author |
---|---|---|---|
#56 | 1946240-56.patch | 2.76 KB | rpayanm |
#56 | 1946240-56-test_only.patch | 2.41 KB | rpayanm |
Comments
Comment #1
rszrama CreditAttribution: rszrama commentedQuick patch, though certainly not the only approach. It's worth noting that the function doesn't really care what the index is, it just made an unsafe assumption.
Comment #2
larowlanWorks for me, doesn't need to go into 8.x because #1939082: Convert theme_status_messages() to Twig will remove that function altogether
Comment #3
David_Rothstein CreditAttribution: David_Rothstein commentedIf the code exists in Drupal 8, it needs to be committed to Drupal 8 first.
Also, if I'm reading #1939082: Convert theme_status_messages() to Twig correctly, won't the current patch in that issue simply wind up taking this bug and porting it over to the Twig template? :)
Comment #4
David_Rothstein CreditAttribution: David_Rothstein commentedI wouldn't have thought of this except for the above... but could this use tests?
Maybe it's a bit of overkill to test this, but on the other hand, I certainly find myself altering/removing status messages all the time, so having a test to ensure that you can do that without the display breaking might be reasonable.
Beyond that, the patch certainly looks good.
Comment #5
rszrama CreditAttribution: rszrama commentedAs far as I understood it, the method for Drupal 8 would either be a different Twig variable operator or else a different patch to the preprocess function to actually reindex the messages array. In either case, the D7 patch is irrelevant to D8 and I proposed the fix in #1939082-8: Convert theme_status_messages() to Twig.
I thought about tests as well but wasn't sure what you'd be testing... in this case, that reset() worked as expected?
Comment #6
David_Rothstein CreditAttribution: David_Rothstein commentedI was thinking of a functional test, something like this.
Comment #7
David_Rothstein CreditAttribution: David_Rothstein commentedWhile writing this, though, I realized that the above test passes/fails as expected only because of this:
However, do we really support that?
If you instead do it using the API (which, granted, involves a lot less straightforward code):
Then everything works and there is no bug.
But then again, this issue is filed as a task rather than a bug report :) And I think switching to reset() is a reasonable idea anyway.
Comment #8
jcisio CreditAttribution: jcisio commented#7 David, this issue came from #1945758: Work around theme_status_messages() limitation to display checkout form errors outside panes.
Comment #9
David_Rothstein CreditAttribution: David_Rothstein commentedAh, I see. But in the end, looking at http://api.drupalize.me/api/drupal/function/commerce_checkout_form_valid... it still seems to do something similar:
So ultimately you still only hit an issue here if you manipulate $_SESSION['messages'] directly, rather than calling drupal_set_message() to re-set the messages you want to keep around.
Question I guess is whether we want to "say it's OK" to manipulate $_SESSION['messages'] directly by asserting things about that behavior in a test.
Comment #10
ExTexan CreditAttribution: ExTexan commentedHow about backporting this patch to D6 and (I assume) D7? I had this problem in D6.
Comment #13
star-szrThis is still relevant for 8.0.x. Looking back to #1939082-9: Convert theme_status_messages() to Twig, I think we can simply update the Twig template to use the "first" filter because we are now on Twig 1.15.0: http://twig.sensiolabs.org/doc/filters/first.html
Comment #14
star-szrComment #15
madhusudanmca CreditAttribution: madhusudanmca commentedJust removed 0 index.
Comment #17
Yaron Tal CreditAttribution: Yaron Tal commentedUsing {{ messages|first }} as suggested by Cottser.
Comment #18
Yaron Tal CreditAttribution: Yaron Tal commentedComment #19
eiriksmJust tested this. Works as expected. Should we add a test (ref #6)?
Comment #20
star-szrFor sure, I should have tagged it. Thanks @Yaron Tal and @eiriksm!
Comment #21
eiriksmWith tests (sorry for hijacking the patching @Yaron Tal)
Tests are based on #6, but uses the new folder structure and so on...
Comment #22
star-szrCool, thanks! Can we get a test-only patch please?
Comment #23
eiriksmYup, there we are.
Comment #25
eiriksmFailed as expected. Setting back to needs review.
Comment #26
star-szrGreat, just a couple small tweaks and I think this is good to go. Thanks @eiriksm!
The new standard here is
Contains \Drupal\system…
.Per https://www.drupal.org/node/1354#file.
Need a line between the description line and the @return. https://www.drupal.org/node/1354#general
Comment #27
eiriksmAh, that is all true.
New patch attached.
Comment #28
eiriksmComment #29
star-szrLooks good!
Comment #32
star-szrComment #34
alexpottCommitted 1d70740 and pushed to 8.0.x. Thanks!
Comment #36
er.pushpinderrana CreditAttribution: er.pushpinderrana commentedPatch for D7.
Comment #37
star-szrGreat, thanks @er.pushpinderrana! This needs tests for D7 now, I think there were some earlier in the thread (or we can backport the test that was committed to 8.0.x).
Comment #38
ryan.gibson CreditAttribution: ryan.gibson commentedI'll pick this one up. :)
Comment #39
dgtlife CreditAttribution: dgtlife commentedI combined the respective patches for drupal_set_message() and the associated test functions in Drupal 7
Comment #41
hampercm CreditAttribution: hampercm commentedFixing test in patch from #39
Comment #42
hampercm CreditAttribution: hampercm commentedCorrected the backported test contained in #39 to function with D7 SimpleTest. Also fixed 4 whitespace issues contained in the patch from #39.
Comment #43
star-szrAwesome, thank you both @dgtlife and @hampercm! I think all we need now is a test-only D7 patch uploaded to ensure the test will catch a regression.
Comment #44
hampercm CreditAttribution: hampercm commentedComment #45
hampercm CreditAttribution: hampercm commentedHere is the test-related portion split out from the patch in #42. Is this what you were looking for, @Cottser ?
Comment #47
star-szrThat's great, thank you @hampercm! Just a few small points but otherwise this looks RTBC to me.
Minor: We can remove the blank line here.
It looks like a group of 'System' would be more appropriate, all the 'Bootstrap' tests are in bootstrap.test.
Are these lines in the test needed? Seems like they shouldn't be for testing this functionality.
Comment #48
hampercm CreditAttribution: hampercm commentedThank you @Cottser. Here is the updated patch and test-only patch.
Comment #50
star-szrLooks great, thanks again @hampercm. Tests look good and from my perspective this meets the core gates. RTBC!
Comment #53
star-szrComment #55
star-szrBetter title for D7 commit, and this needs reroll.
Comment #56
rpayanmComment #58
star-szrThanks @rpayanm!
Comment #59
David_Rothstein CreditAttribution: David_Rothstein commentedCommitted to 7.x - thanks!