Obviously the string '0' is not a particularly helpful message and so maybe in real world use cases it would not be done but sometimes I use drupal_set_message on my development server as a quick debugging tool. Setting the message to be the values of variables, I expect to see all the values output in the same way. But if the value passed is a numerical or string zero, then no message at all (not even an empty < li >< /li > pair) is output. This seems wrong!

Sorry that I don't know which component bootstrap.inc belongs to.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

petebarnett’s picture

From reading the docs, I'd say the intended purpose of drupal_set_message(...) is to store user targeted (admin or otherwise) natural language messages. The docs say

...For consistency with other messages, it should begin with a capital letter and end with a period.

Additional contrib tools are available for debugging and displaying PHP primitives in the form of the Devel module, with its dpm() etc.

For that reason, I don't believe this can be considered a bug. On the contrary, if any module was outputting the rather unhelpful message of '0', I would consider that a bug. This case only arises from the undesigned use of the function as a developer tool.

If you're interested, the reason that you can't display '0' is that drupal_set_message() on line 1757 of bootstrap.inc evaluates $message itself, and only proceeds to store the message if that string evaluates to TRUE. In PHP '0' will evaluate to FALSE.

martin_q’s picture

You're right of course, and I was attempting to acknowledge this in my initial post. Whilst '0' shouldn't ever be a valid message, for the reasons you give, the purist in me says that if ($message) in line 1757 of bootstrap.inc is sloppy and using if(isset($message)) would be better practice!

Hmmm, I guess the idea is to avoid printing a message with an empty string (""). I still haven't accepted the idea that "0" should also be considered an empty string. Maybe my beef is with PHP, not Drupal... :)

thehong’s picture

Status: Active » Needs review
Issue tags: +trivial
FileSize
520 bytes
martin_q’s picture

Works for me. As the OP, am I allowed to set it to 'reviewed and tested by the community'?

longwave’s picture

Version: 7.x-dev » 8.x-dev

As trivial as this is, it should be fixed in 8.x first and then backported.

longwave’s picture

Issue tags: -trivial

#3: 1845104.diff queued for re-testing.

Status: Needs review » Needs work
Issue tags: +trivial

The last submitted patch, 1845104.diff, failed testing.

martin_q’s picture

The failure is, as far as I can tell, because bootstrap.inc has moved from includes/ to core/includes/. The line number for the change is now 1801. I am not currently set up with git or D8 though so I can't roll a new diff. I imagine just hand-modifying the file here would be of little use for merging the change in etc, so I won't do that.

longwave’s picture

Status: Needs work » Needs review
FileSize
541 bytes

Not sure if this needs tests; there don't seem to be any other explicit tests for drupal_set_message().

longwave’s picture

Issue summary: View changes

Added spaces to prevent html tags being taken literally.

marabak’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: +Amsterdam2014

the patch #9 is working fine.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 74857b5 and pushed to 8.0.x. Thanks!

  • alexpott committed 74857b5 on 8.0.x
    Issue #1845104 by longwave, thehong | martin_q: Fixed drupal_set_message...
longwave’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Fixed » Needs review

#3 could still be committed to 7.x, if anyone really cares.

longwave queued 3: 1845104.diff for re-testing.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

#3 is RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: 1845104-drupal_set_message-zero.patch, failed testing.

dcam’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
520 bytes

Re-uploading #3 so Testbot quits re-testing the D8 patch. I am not the author and it should not be attributed to me. Setting this back to RTBC per #16.

David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs review

I did a quick test of drupal_set_message('') with this patch and it looked really messed up on a couple different themes.

At least for Drupal 7 perhaps we should continue to not print anything in that case (or drupal_set_message(FALSE) too?) to avoid any such hidden bugs in people's code popping up on real sites...?

So maybe something like if ($message || $message === '0')...

David_Rothstein’s picture

Or maybe if ($message || $message === '0' || $message === 0) to be really robust...

er.pushpinderrana’s picture

Incorporated #20 recommendation in this patch.

Status: Needs review » Needs work

The last submitted patch, 21: D7-1845104-21-drupal_set_message-zero.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Issue tags: -trivial, -Amsterdam2014 +Novice

That was a random test failure.

dcam’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
2.14 KB

With the patch applied '0' and 0 can be passed as the message parameter in drupal_set_message().

rukayya’s picture

the patch #9 is worked for me

David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +7.40 release notes

Committed to 7.x - thanks!

Adding this to the release notes since it is a slight behavior change in drupal_set_message(), in particular with regard to whether the function returns messages or not.

  • David_Rothstein committed f09fb9e on 7.x
    Issue #1845104 by dcam, longwave, thehong, er.pushpinderrana, martin_q,...
Studiographene’s picture

the patch #9 is worked for me

Thanks

Status: Fixed » Closed (fixed)

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