Updated: Comment 26
Problem/Motivation
#577424: The zero incident highlighted that we should double check our loose comparisons.
Here is the first bug I stumbled upon: the page title is not displayed if it is equal to "0".
Steps to reproduce: Create a new node, type "0" as a title, save. Title "0" is not displayed
Proposed resolution
Check title existence in page.html.twig; instead of it's emptiness
Remaining tasks
None
User interface changes
No UI changes
API changes
No API changes

| Comment | File | Size | Author |
|---|---|---|---|
| #132 | node-title-578400-132.patch | 6.23 KB | hgoto |
| #132 | node-title-578400-132-test_only_should_fail.patch | 1.48 KB | hgoto |
| #124 | node-title-578400-124.patch | 5.16 KB | therealssj |
| #116 | interdiff-578400-82-116.txt | 4.37 KB | therealssj |
| #116 | node-title-578400-116.patch | 5.21 KB | therealssj |
Comments
Comment #1
damien tournoud commentedWe need something like that ;)
Comment #3
webchickTest please. :) Let's make sure we don't have this embarrassing bug anymore. ;)
Comment #4
effulgentsia commentedThis is an excellent issue for someone new to rolling patches and writing tests to get their hands dirty with. Anyone game to try it?
Comment #5
sign commentedre-rolled
Comment #7
idflood commentedWhile reproducing this bug i've found another issue. I can't "provide a menu link" with a "Menu link title" value == 0. Should I fill another issue for that?
This patch brings back check_plain() on the title, but I had to change page.tpl.php in themes ( doing if(isset($title)) instead of if($title)).
Comment #9
idflood commented#7: 578400-7-empty-page-title.patch queued for re-testing.
Comment #10
gábor hojtsy1. Tested the patch.
2. Checked out the latest version of Drupal HEAD as of a few minutes ago.
3. Reproduced the Garland issue with an article node.
4. Reproduced the issue in Seven by editing the menu title for "admin/content", and verifying it displays 0 in the toolbar and disappears in Seven.
5. Applied the patch.
6. Verified that the patch fixed the issue with the node (in Garland) and the admin page displayed in Seven.
7. Also did a code review and the code looks good.
I think this is all good to go.
Comment #11
gábor hojtsyHad plenty of time to create a simple test for this. Let's prove it fails without the patch. This one creates a node titled '0' and fails in Garland.
Comment #13
gábor hojtsyOk, so now that we proved we have a test which shows it failing, let's include that test with the patch and show it works. Only change included in test is fix for some comment copy-paste. Let's see now.
Comment #14
effulgentsia commentedI think I prefer Damien's #1 patch that used strlen(), so that an empty string is considered the same as NULL, especially since as I read drupal_get_title(), it never returns NULL, unless in bootstrap. Unless we want to consider empty string as a valid title, in which case we need to change check_plain(NULL) to return NULL or not have drupal_get_title() call check_plain() if menu_get_active_title() returns NULL. In any case, let's add whatever we decide with respect to empty string to our test, and make sure our test also includes a situation where menu_get_active_title() returns NULL.
I guess this makes the issue no longer a "Novice" one.
[EDIT: if we decide to leave empty string title as equivalent to a NULL title, let's put the strlen() check into template_preprocess_page(), so that page.tpl.php can be left using isset().]
Powered by Dreditor.
Comment #15
gábor hojtsyDisclaimer: we found this issue for a live session demonstrating patching and test writing as well as issue collaboration. It proved to be a perfect use case for it. So let's make this even better and do not refrain from taking the patches apart. No offense :)
Comment #17
valthebaldLet's check first if the issue affects D8. Update: it does
Comment #18
valthebaldComment #19
valthebaldComment #20
ekl1773Comment #21
melsi commentedThe patch is missing the title tag. Added that part.
Comment #23
melsi commentedSecond try
Comment #24
dawehnerLet's send it to the testbot.
Comment #25
valthebaldStill needs testing
Comment #26
valthebaldComment #27
grisendo commentedComment #28
valthebaldComment #29
aaronschachter commentedI'm at the Austin sprint and working on a test for this.
Comment #30
aaronschachter commentedComment #31
aaronschachter commentedUpdated the patch in #23 per line changes, and added tests for edge case where node title is set to 0.
Comment #32
star-szrThanks @aaronschachter! Test case looks good at least from reading it, one nitpick:
You can use assertTitle() here to make this a little shorter :)
--
If you can please post a test-only version followed by a complete patch that would be great, then we can see the fail/pass because the test should fail without the fix. As pictured on https://drupal.org/contributor-tasks/write-tests :)
Comment #33
aaronschachter commentedSure @cottser I can re-post... Buuut... I was following the same pattern that already exists in the NodeTitleTest, in line 53-54:
What's considered best practice here? It would make sense to update that original
assertEqualstatement I copied from as well... but that's not really within the scope of this issue. Is it best practice to create a separate issue for it and leave the original as is?Comment #34
aaronschachter commentedHmm, so it seems like the patch in #23 which I modified to update line numbers in #31 isn't even necessary. I'm using the latest 8.x branch, and the node title correctly displays as 0 without patching anything.
This was my first attempt at submitting a patch to core, and I am realizing today, I did not actually test whether or not the 0 page title worked from the start :|
Hard to say whether or not it was broken at the time of #23, but anyhoo here are some tests to hopefully close this issue.
Comment #35
star-szrI did a
git bisectand confirmed that this was fixed in #2216437: Entity labels are not in-place editable on "full entity page" (prime example: node title). So let's add this test and then go to 7.x which probably needs a bugfix + tests.Sounds good to me.
Comment #37
catchCommitted/pushed to 8.x, thanks!
Moving to 7.x for backport.
Comment #38
joshi.rohit100Patch for D7.
Please check.
Comment #39
star-szrThanks @joshi.rohit100. If we can get a test-only version to see that the test fails without the fix that would be very helpful. An example of this can be seen on https://www.drupal.org/contributor-tasks/write-tests (below point 13).
Won't $title always be set here?
Looks like a stray tab character here, please remove per https://drupal.org/coding-standards#indenting.
Comment #40
amitgoyal commentedPlease see test-only version of the patch which also removes stray tab character there.
Comment #42
star-szrThank you @amitgoyal!
Nevermind my #39.1, didn't realize that title would be null (check out drupal_set_title()). So now that we've seen the test fail we need a complete patch without the stray tab character.
Comment #43
joshi.rohit100Updated the patch. Please review now.
Comment #44
star-szrAh, @amitgoyal's patch fixed this but we need to add a space here after the
//and before "Test edge case…" per https://www.drupal.org/node/1354#inline.Otherwise looks good :)
Comment #45
pushpinderchauhan commentedUpdated patch as @Cottser suggested in #44.
Comment #46
star-szrLooks good, thanks!
Comment #47
star-szrHmm, actually I went through manually and tested this and I think this patch is supposed to fix not just the title tag but also the
<h1>when viewing a node with a title of '0', and the D7 patch only fixes the first case. This also exposes a weakness in the test (we may need to revisit the 8.x test to make it more solid) because the test-only patch in #40 should have 2 fails.Comment #48
gvorbeck commentedI'm new to fixing issues but I thought I'd give it a try. I fixed it within the bartik theme.
Comment #49
pushpinderchauhan commented@gvorbeck, thanks!
Once you add any patch also change Status from Need Work to Need Review.
Comment #51
sivaji_ganesh_jojodae commentedRe-rolled patch #48.
Comment #53
mtomaizy commentedComment #56
valthebaldComment #57
joshi.rohit100Re-rolled #51 but it seems same
Comment #59
jmev commentedI'm new to patching/testing, but have manually tested the patch and can confirm it works. Is there a possibility that the test itself is faulty? Is it possible to get a copy of that for review?
Also, I looked for the reference in modules/node/node.test mentioned by @Cottser on comment #44 (https://www.drupal.org/node/578400#comment-8946193) but I can't find that comment, so I'm wondering if I have the same files.
Sorry if my comment isn't helpful, trying to get up to speed here to make some useful contributions.
Comment #61
dcam commentedThe D7 Testbot will occasionally crash during a test for some reason. If you're not sure why a test failed, check the results. If the failure was in an unrelated system and it says "The test failed to complete for some reason," then it's a good bet that it was one of these random failures. In that case, just re-test the patch.
Comment #65
dcam commentedComment #66
makbul_khan8 commentedDoing changes in .tpl.php will be restricted to theme specific. Adding code on includes/theme.inc will resolve this specific problem of "Display the page title, even if 0".
Comment #68
dinarcon commentedThanks @makbul_khan8. Here is some feedback.
Shall we check for
$variables['title'] === "0"instead?These need some updates to conform to the coding standargs https://www.drupal.org/coding-standards
* Add a space after if
* Add a space before the string concatenation operator
* Comment should end with a full stop (period)
Comment #71
mw4ll4c3 commentedTackling this for #devdays
Comment #72
mw4ll4c3 commentedRe-rolled, fixed the whitespace issue, and used === '0' as suggested in #68, since lots of things can == 0
(Hopefully the previous FAILED was just a "server burp.")
Comment #73
star-szrThank you @mw4ll4c3!
We don't usually include links to d.o issues in code comments unless they are @todos.
This doesn't seem like a proper fix to me, it's just adding whitespace.
Comment #74
yogen.prasad commentedComment #75
yogen.prasad commentedComment #76
travis-bradbury commentedI agree with Cottser that adding a space to the title isn't a proper solution. I don't see any other way to display a title of '0' except changing the conditions in the template files from
if ($title)to something likeif (is_string($title)). This could mean that quite a few templates or themes need to change if they want to handle this edge case.#45 and others used
isset($title)which I think should work in all cases butis_string($title)does a little better to indicate what we're testing for.#40 appears to be the most recent test only patch so I started there. It had an issue where the second test passed when it shouldn't. I believe I've fixed that. Here's a test only patch and and interdiff with 40.
Comment #77
travis-bradbury commentedTrigger testbot
Comment #79
travis-bradbury commentedA new full patch and interdiff with full patch #45.
Comment #80
travis-bradbury commentedForgot status for testbot's attention again.
Edit: looks like I've got a mistake still.
Debug outputs:
Manually testing shows that the page title is omitted before the patch and present after, but I gather from the test's debug() that it IS outputting an H1 with id of "page-title" but the element is empty.
I'm not sure if that's a problem in my test or a problem in the template outputting the title.
Comment #82
travis-bradbury commentedShould have it fixed this time. Local testing has the test only patch failing both tests with both passing after the full patch.
Comment #84
gatorjoe commentedI reviewed #82 on a 7.38 install and confirmed that the page title "0" does appear. This patch was successful for me.
Comment #85
HeimdallJHM commentedLast patch works properly, attaching images
Comment #86
HeimdallJHM commentedComment #87
David_Rothstein commentedPutting is_string() throughout all tpl.php files like that really doesn't look like the right solution. Also, are we really sure you can never pass an integer to drupal_get_title() and have it work?
Maybe isset() rather than is_string() would work, though if the solution involves changing the tpl.php files then it still means this won't work with lots of non-core themes unless they update too. It's such an edge case anyway, maybe we shouldn't fix it at all if we can't fix it consistently? Or fix the $head_title part only, since at least that's simpler?
Comment #88
shubhangibb commentedComment #89
shubhangibb commentedComment #90
shubhangibb commentedComment #91
cchanana commentedComment #92
cchanana commentedComment #93
sowmiya.S commentedHi,
I have worked in article content type to display the title as "0", its working fine.
Comment #94
joyceg commentedTested the patch.
It works for me.
Comment #95
joyceg commentedAfter applying the patch.
Comment #96
joyceg commentedI would like to work on this issue.
Comment #97
joyceg commentedThis patch works for me.
Comment #98
joyceg commentedComment #99
tim.plunkettI always refer to http://php.net/manual/en/types.comparisons.php
if ($title)will never work for 0 or "0".Also, changing to use either isset/is_string will cause an empty string to print, which would be an even more problematic regression.
Comment #100
joyceg commentedAdding the inter diff and the new patch.
Comment #102
joyceg commentedI am not able to understand this error.
Can someone help me explain the defect of this patch?
Comment #103
travis-bradbury commentedThe failing test is looking for the title output on the page (as opposed to in the head). I think that your problem is here:
We'd need to compare to the string 0, not integer 0.
Comment #104
karthikkumarbodu commentedFixed the string comparison and re-uploaded patch.
Comment #106
karthikkumarbodu commentedFixed review comments and uploading patch.
Comment #107
therealssj commentedComment #109
therealssj commentedWhy don't we use strlen to check for the title length.
It works for all the possible cases while skipping empty strings.
Comment #110
joyceg commentedUpdated the patch. Uploading the interdiff too.
This worked for me.
Adding the screenshot along with the patch.
Comment #111
joyceg commentedComment #113
joyceg commentedComment #114
joyceg commentedComment #116
therealssj commentedAnother try, let's see.
Using strlen(trim($title)) as the check
Comment #117
therealssj commentedComment #119
karthikkumarbodu commentedAll tests are passing. Good work Mehul
Comment #120
joyceg commented@therealssj, Nice work. The patch was tested successfully.
Comment #121
valthebaldRemoving 'Needs tests' (since there is a test now)
Comment #122
fabianx commentedI know this issue is really old and I know the approach was verified, but I really don't want to spend 6 function calls for such an edge case on every page.
I would suggest:
Then use isset($title) in page.tpl.php, which also matches the original twig solution with "title is defined".
Explanation:
isset() costs almost zero performance as it is a build-in, but strlen(trim()) costs 2 function calls and two string traversals.
Comment #123
therealssj commented@Fabianx the reason we didn't go with isset in the first place was bcoz @tim.plunkett pointed out that it would cause regression with allowing the printing of empty strings ( though I am not sure how would one be able to save an empty string )
Other than that using isset($title) in the twig files seems to be correct and using strlen(trim()) there is useless.
I have changed this in the current patch.
Comment #124
therealssj commentedAttached wrong patch file before.
Comment #125
fabianx commented#124:
Thank you for your patch!
Unfortunately you missed the else { } part and timplunkett is right, but my else {} part in #122 ensures that if the title is empty it is unset and isset() works then.
Comment #126
therealssj commented@Fabianx I don't think we need that
$title = NULLin the else part.This is how the following lines look
I am not sure how
$head_titleis processed further but from the above lines it does look like that we pass$titlein the$head_titlearray and in the else we are not passing it so by default I guess it becomes null.As for why I didn't go with
isset($title) && $title !== ''is that it wouldn't work for a string like ' ', I maybe over thinking so I would like to know what you think.Comment #127
fabianx commented#126: The problem is:
Lets assume drupal_get_title() returns ''.
Now do:
works as it won't display the title.
won't work as it would show that is has a title, but it is empty actually.
Therefore the solution is to do above:
Afterwards:
works as it won't display the title, as NULL fails this check (BC for existing templates).
works too as the title is actually NULL, which isset() returns as FALSE.
Does that make the plan more clear why we need a $title = NULL?
Thank you for your work on this!
Comment #128
costellofax commented@Fabianx, as @therealssj pointed out, there might not be any way in the UI to ever save an empty string in the required Title field, so coding for $title = NULL seems unnecessary?
Comment #129
fabianx commented#128 There is many possibilities for title to get to be '', not just the UI, so the code will be needed to work like this for BC reasons.
Comment #132
hgoto commentedI'd like to chime in and proceed on this issue.
If I understand it correctly, the situation is as following, right?
as is:
to be:
I believe this patch will work If my recognition above is correct.
As Fabianx explained, we need to prevent empty titles to be displayed. Since
$titlein the page.tpl.php template is prepared not intemlate_preprocess_html()but intemplate_process_page()and I added the following lines into the patch instead of adding else section intotemlate_preprocess_html(). I believe this is a right approach.In addition, I think it's better to add tests for the cases 2 and 3 above (we have a test for the case 1 in the previous patch). So I added such tests, too.
I'm sorry to interrupt. I'd like someone to review this. Thank you.
Comment #133
hgoto commentedComment #138
fabianx commentedThanks for your work on this!
Nice work on the ?: I almost missed that you implemented my solution :).
I wonder why tests fail though ...
Comment #140
hgoto commentedI'm sorry to have tried the test again and again. There seems to be errors randomly but the patch passed the tests after a few trial!
@Fabianx thank you for your reply! Theme builders may use
if ($title) { ... }pattern in page.tpl.php in their custom themes and we should keep the existence of$titlevariable with value NULL in page.tpl.php whenstrlen(trim($title))becomes 0 exactly as you told.The status of this issue became "Needs work" because of the random failure and I'd like to move this to NR again.
Comment #141
fabianx commentedThis is even RTBC - indeed just random failures! Great work!
I would like a "change record" or an documentation update, where we state that isset() is preferred for checking title variables to ensure that "0" and other evaluation to FALSE can be properly displayed.
Not really sure where.
Will discuss with Stefan and David at some time.
Besides this, this looks really good! Thanks so much!
Comment #142
stefan.r commentedComment #144
stefan.r commentedCommitted and pushed to 7.x, thanks!
This still needs a change record.
Comment #145
fabianx commentedAdding for release notes, tag
Comment #146
David_Rothstein commentedIn addition to the missing change record, I think there's another problem here that needs more discussion. Therefore I rolled this patch back for now.
Currently in Drupal, you can do something like
$variables['title'] = ''in a preprocess function and that will suppress the title - that is pretty reasonable code.But with this patch, the title is only modified to NULL in some cases not others, so won't the above code stop working and result in the title HTML being printed to the page (and potentially breaking the layout)?
I think we need a solution that allows the above code to still work.
Also, the isset() in templates is a bit ugly... seems like it would be cleaner to define a new (boolean) $has_title variable and check that?
Comment #148
fabianx commentedI do agree with the reasoning for the revert.
I think we can instead solve this with an invisible UTF-8 character to prevent PHP from evaluating "0" to FALSE:
e.g. http://www.fileformat.info/info/unicode/char/200B/index.htm