Found character U+0000 (NULL) in output, at call to module_invoke, immediately before the normal block content:

$my_element = module_invoke('my_module', 'block_view', 'my_block_id');
print render($my_element);

This was reported by HTML validator. I checked all my source files and there's no U+0000 in my template or module.

CommentFileSizeAuthor
#2 drupal-fix-for-null-char-2559591-2-D7.patch516 bytesttread

Comments

ttread created an issue. See original summary.

ttread’s picture

StatusFileSize
new516 bytes

Patch that fixes this problem. It prevents drupal_render from processing non-arrays.

cilefen’s picture

I am just curious. How is a non-array arriving from your module_invoke in the first place?

cilefen’s picture

Status: Active » Needs review
cilefen’s picture

Title: Character U+0000 in HTML output » Character U+0000 in HTML output when a non-array is sent to render()
ttread’s picture

Traced this in the debugger. The problem occurs on the call to print render(). In my module's hook_block_view, $block['subject'] is set to a string as per the API documentation, with the render array in $block['content']. Print render() calls drupal_render(), which on line 5977 sets the variable $children to ['subject', 'content']. When drupal_render calls itself recursively on its children on line 5994, the first child, the string subject, is passed in where it's expecting an array.

I don't think this problem has anything to do with my module code, and suspect it could be affecting other sites, unless I've overlooked something.

cilefen’s picture

Could we see your implementation of hook_block_view?

ttread’s picture

I just realized what I did wrong, in the template I used print render($my_element) when I should have used print render($my_element['content']). So this is my error.

Nonetheless, this patch #2 could be useful to prevent problems when others make this mistake.

cilefen’s picture

Arguably you would want to throw an exception here.

It's funny, on a Drupal 8 issue I asked @joelpittet a deeper question: "How do you know if an array you have been passed is a render array?" It is not trivial.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.