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';
  }
CommentFileSizeAuthor
#9 998612-render-0-markup.patch832 byteslongwave

Comments

gcobos’s picture

Component: markup » field system
yched’s picture

Component: field system » base system

Not related to the Field API.

avpaderno’s picture

The description for #markup reported in http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.... is the following:

Used to set HTML that will be output on the form.

I get the attribute is expecting a string, not an integer.

avpaderno’s picture

Title: drupal_render doesn't render a (int)0 when set into #markup property » drupal_render() doesn't render a (int)0 when set into #markup property
marcingy’s picture

Priority: Major » Normal

This is at best normal if not minor.

avpaderno’s picture

Status: Active » Closed (works as designed)
Issue tags: -not rendering, -drupal_render

I would say it works as expected, as the attribute requires a string, not an integer.

gcobos’s picture

Component: base system » field system
Status: Closed (works as designed) » Needs review
Issue tags: +drupal_render

I 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']) "

moshe weitzman’s picture

isset($elements['#markup']) looks good to me.

longwave’s picture

Title: drupal_render() doesn't render a (int)0 when set into #markup property » drupal_render() doesn't render 0 when set into #markup property
StatusFileSize
new832 bytes

Just 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.

yched’s picture

Component: field system » forms system
Issue tags: -drupal_render

Moving out of the Field API queue.

bfroehle’s picture

Status: Needs review » Reviewed & tested by the community

Passes 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:

$element = array(
  '#markup' => '0',
);
print drupal_render($element);

should display '0', not ''.

dawehner’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7
dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x and 8.x. Thanks.

bfroehle’s picture

Issue tags: -Needs backport to D7

Untagging since it's been committed to 7.x

Status: Fixed » Closed (fixed)

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