Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Using #plain_text, these render arrays will not display the expected values (they render as empty string):
$render_arrays = array(
array('#plain_text' => "0"),
array('#plain_text' => 0)
);
But this works:
$render_arrays = array(
array('#markup' => "0"),
array('#markup' => 0)
);
I want to use the #plain_text attribute since I want the value to be escaped (e.g. user input).
Comment | File | Size | Author |
---|---|---|---|
#18 | interdiff_15_18.txt | 1.9 KB | c31ck |
#18 | fix_plain_text_empty_rendering-2765609-18.patch | 2.83 KB | c31ck |
#18 | fix_plain_text_empty_rendering-2765609-18_test_only_should_fail.patch | 1.7 KB | c31ck |
#15 | interdiff-2765609-11-15.txt | 1.8 KB | yogeshmpawar |
#15 | 2765609-15.patch | 2.42 KB | yogeshmpawar |
Comments
Comment #2
weboide CreditAttribution: weboide commentedComment #3
weboide CreditAttribution: weboide commentedComment #4
hgoto CreditAttribution: hgoto as a volunteer and at Studio Umi commentedThank you for sharing the issue. I tested and confirmed that this bug exists.
This seems to be caused as
Drupal\Core\Render\Renderer::doRender()
doesn't check the value of #plain_text properly.Here
empty()
is not proper and we should useisset()
instead, I think.I created a patch. I thought it's better to add tests for the following cases and added such tests, too.
I'd like someone to review this. Thank you.
Comment #9
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThis patch and test make complete sense to me. I just ran into this issue while working on #2922896: Write dedicated rendering tests. I am rerunning the tests and then I am going mark this RTBC. Hopefully, it will get committed or someone will ask for more test coverage.
Comment #11
gnugetToday I ran into this issue as well.
So, here a rebased version of the patch.
Comment #13
gnugetIt seems that the patch needs to fix some coding standard problems.
I'm going to add the
novice
tag in case someone wants to do it.Comment #14
yogeshmpawarComment #15
yogeshmpawarChanges addressed in comment #13 & also added an interdiff.
Comment #16
gnugetLooks great, thanks!
Comment #17
alexpottNice find. I think we should try to keep the #markup and #plain_text logic as similar as possible.
Let's change both
!empty()<code> to <code>isset()
This whole check seems pointless.
Note that we don't need to check the
isset($elements['#markup']
in the second condition because it must be set to have got here. I think reviewing this code we should make this fix but also open a follow-up issue to inline ensureMarkupIsSafe into the main render function because it is not needed as a separate function. It just makes it most complex.If we inlined the function it would look like:
which in my opinion is easier to grok than having the misdirection of the method call inside the
if()
. But that is all followup material.Comment #18
c31ck CreditAttribution: c31ck commentedAttached a patch that addresses remarks 1, 2 and 4. I've added a check for '' and NULL to the tests. Also attached a patch containing only the test and an interdiff.
Comment #20
gnugetLooks great.
Thanks!
Comment #21
alexpottCrediting @weboide for discovering and filing the issue. Crediting myself for a review that directly influenced the patch.
Comment #22
alexpottCommitted and pushed 018bd7c5e2 to 8.6.x and 1675b7b81c to 8.5.x. Thanks!