When try to visualize a custom field in a table view, which has a integer value of 0, (not a string), drupal_render() renders an empty string instead a '0'. The case is exactly reproduced with:
$elements = array('#markup' => 0);
drupal_render($elements);
Maybe the value in #markup should be always a string, so it would be an error from the field, but, if a integer is allowed in #markup, then a solution should be simply change in the following lines, that
if (!empty($elements['#markup'])
for a
if (isset($elements['#markup'])
That change allows the #markup to be a 0 integer and to be rendered correctly.
// If #markup is not empty, set #type. This allows to specify just #markup on
// an element without setting #type.
if (!empty($elements['#markup']) && !isset($elements['#type'])) {
$elements['#type'] = 'markup';
}
Comments
Comment #1
gcobos commentedComment #2
yched commentedNot related to the Field API.
Comment #3
avpadernoThe description for
#markupreported in http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.... is the following:I get the attribute is expecting a string, not an integer.
Comment #4
avpadernoComment #5
marcingy commentedThis is at best normal if not minor.
Comment #6
avpadernoI would say it works as expected, as the attribute requires a string, not an integer.
Comment #7
gcobos commentedI included that example to be able to reproduce the error. I'm not trying to send a 0(integer) to the drupal_render function. The case is that drupal does.
I have a clean Drupal 7 and a field "list_number" added to the default content type 'article'. The options of that field goes from 0 to 10.
Well, if I create a content (type 'article') and select the value 0 for that field, and then render the content, the '0' value is not printed.
As you said, drupal_render() can be right, because that value in #markup property should be a string, but then, there's an error in the field system, because a 0 value from a numeric select_list will not show up.
I think the easiest way to fix this error is changing the "!empty($elements['#markup']) " for a "isset($elements['#markup']) "
Comment #8
moshe weitzman commentedisset($elements['#markup']) looks good to me.
Comment #9
longwaveJust ran into this when testing Ubercart, where I noticed 0 wasn't showing up when rendered using #markup.
Note that even if the 0 is a string, empty('0') is still TRUE. The proposed fix works for me, so I made a patch.
Comment #10
yched commentedMoving out of the Field API queue.
Comment #11
bfroehle commentedPasses SimpleTests, @moshe signed off, and @longwave is correct that many thing, including strings like '0' are interpreted as empty (see http://us2.php.net/empty for a complete list).
Patch here will allow drupal_render to correctly, for example:
should display '0', not ''.
Comment #12
dawehnerComment #13
dries commentedCommitted to 7.x and 8.x. Thanks.
Comment #14
bfroehle commentedUntagging since it's been committed to 7.x