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

CommentFileSizeAuthor
#132 node-title-578400-132.patch6.23 KBhgoto
#132 node-title-578400-132-test_only_should_fail.patch1.48 KBhgoto
#124 node-title-578400-124.patch5.16 KBtherealssj
#123 node-title-578400-123.patch3.9 KBtherealssj
#120 nodeTitle0.png15.91 KBjoyceg
#116 interdiff-578400-82-116.txt4.37 KBtherealssj
#116 node-title-578400-116.patch5.21 KBtherealssj
#116 578400-116-test-only.patch867 bytestherealssj
#113 interdiff-578400-82-113.txt54.83 KBjoyceg
#113 578400-113.patch58.96 KBjoyceg
#110 TitleZero.png19.96 KBjoyceg
#110 578400-109.patch21.13 KBjoyceg
#110 interdiff-578400-82-578400-109.txt20.29 KBjoyceg
#106 core-fix-page-title-display-578400-106-7.x-dev.patch5.41 KBkarthikkumarbodu
#104 core-fix-page-title-display-578400-104-7.x-dev.patch5.41 KBkarthikkumarbodu
#100 interdiff-578400-82-578400-100.txt4.45 KBjoyceg
#100 578400-100.patch5.29 KBjoyceg
#97 displayPageTitleEven0-578400-97.patch5.16 KBjoyceg
#95 title.png11.18 KBjoyceg
#85 display-page-title-even-if-0-578400-82-after.jpg22.71 KBHeimdallJHM
#85 display-page-title-even-if-0-578400-82-before.jpg19.21 KBHeimdallJHM
#82 display-page-title-even-if-0-578400-82.patch5.18 KBtravis-bradbury
#82 display-page-title-even-if-0-578400-82-test-only.patch867 bytestravis-bradbury
#79 interdiff-578400-45-79.txt4.05 KBtravis-bradbury
#79 display-page-title-even-if-0-578400-79.patch4.71 KBtravis-bradbury
#76 interdiff-578400-40-76.txt640 bytestravis-bradbury
#76 display-page-title-even-if-0-test-only-578400-76.patch826 bytestravis-bradbury
#72 d7_page_title_even_if_0-n578400-72.patch570 bytesmw4ll4c3
#66 core-page-title-even-if-title-is-0-578400-66.patch564 bytesmakbul_khan8
#57 display-page-title-578400-57.patch591 bytesjoshi.rohit100
#51 display-page-title-578400-51.patch591 bytessivaji_ganesh_jojodae
#48 drupal-578400-44.patch937 bytesgvorbeck
#45 interdiff-578400-43-45.txt451 bytespushpinderchauhan
#45 d7-578400-45.patch3.71 KBpushpinderchauhan
#43 interdiff-578400-43.txt372 bytesjoshi.rohit100
#43 d7-578400-43.patch3.71 KBjoshi.rohit100
#40 578400-40-tests.patch794 bytesamitgoyal
#38 d7-578400-38.patch3.71 KBjoshi.rohit100
#34 578400-34-tests.patch920 bytesaaronschachter
#31 interdiff-578400-23-31.txt2.25 KBaaronschachter
#31 578400-31.patch2.63 KBaaronschachter
#23 578400-23.patch2.7 KBmelsi
#21 578400.patch2.7 KBmelsi
#18 578400-17.patch2.25 KBvalthebald
#13 zero-patch-with-tests-comment-fixed.patch4.66 KBgábor hojtsy
#11 title-zero-hero.patch1.39 KBgábor hojtsy
#7 578400-7-empty-page-title.patch2.58 KBidflood
#5 578400-5-empty-page-title.patch1.17 KBsign
#1 578400-empty-page-title.patch1.38 KBdamien tournoud

Comments

damien tournoud’s picture

Status: Active » Needs review
StatusFileSize
new1.38 KB

We need something like that ;)

Status: Needs review » Needs work

The last submitted patch failed testing.

webchick’s picture

Test please. :) Let's make sure we don't have this embarrassing bug anymore. ;)

effulgentsia’s picture

Issue tags: +Novice

This is an excellent issue for someone new to rolling patches and writing tests to get their hands dirty with. Anyone game to try it?

sign’s picture

Status: Needs work » Needs review
StatusFileSize
new1.17 KB

re-rolled

Status: Needs review » Needs work

The last submitted patch, 578400-5-empty-page-title.patch, failed testing.

idflood’s picture

Status: Needs work » Needs review
StatusFileSize
new2.58 KB

While 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)).

Status: Needs review » Needs work
Issue tags: -Novice

The last submitted patch, 578400-7-empty-page-title.patch, failed testing.

idflood’s picture

Status: Needs work » Needs review
Issue tags: +Novice

#7: 578400-7-empty-page-title.patch queued for re-testing.

gábor hojtsy’s picture

Status: Needs review » Reviewed & tested by the community

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

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.39 KB

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

Status: Needs review » Needs work

The last submitted patch, title-zero-hero.patch, failed testing.

gábor hojtsy’s picture

Status: Needs work » Needs review
StatusFileSize
new4.66 KB

Ok, 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.

effulgentsia’s picture

Status: Needs review » Needs work
Issue tags: -Novice
+++ includes/theme.inc	25 Jun 2010 13:23:21 -0000
@@ -2176,8 +2176,9 @@ function template_preprocess_html(&$vari
+  if (isset($title)) {
...
+++ includes/theme.inc	25 Jun 2010 13:23:21 -0000
@@ -2380,8 +2381,9 @@ function template_preprocess_maintenance
+  if (isset($title)) {
...
+++ themes/garland/page.tpl.php	25 Jun 2010 13:23:21 -0000
@@ -43,7 +43,7 @@
+          <?php if (isset($title)): ?>
...
+++ themes/seven/page.tpl.php	25 Jun 2010 13:23:21 -0000
@@ -5,7 +5,7 @@
+    <?php if (isset($title)): ?>

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

gábor hojtsy’s picture

Disclaimer: 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 :)

valthebald’s picture

Version: 7.x-dev » 8.x-dev
Issue summary: View changes

Let's check first if the issue affects D8. Update: it does

valthebald’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Novice
StatusFileSize
new2.25 KB
valthebald’s picture

Issue tags: +Needs backport to 7.x
ekl1773’s picture

Issue tags: +Needs tests
melsi’s picture

StatusFileSize
new2.7 KB

The patch is missing the title tag. Added that part.

Status: Needs review » Needs work

The last submitted patch, 21: 578400.patch, failed testing.

melsi’s picture

StatusFileSize
new2.7 KB

Second try

dawehner’s picture

Status: Needs work » Needs review

Let's send it to the testbot.

valthebald’s picture

Status: Needs review » Needs work

Still needs testing

valthebald’s picture

Issue summary: View changes
grisendo’s picture

Issue tags: - +DrupalCampSpain
valthebald’s picture

Issue tags: -DrupalCampSpain +Novice
aaronschachter’s picture

I'm at the Austin sprint and working on a test for this.

aaronschachter’s picture

Assigned: Unassigned » aaronschachter
aaronschachter’s picture

Assigned: aaronschachter » Unassigned
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new2.63 KB
new2.25 KB

Updated the patch in #23 per line changes, and added tests for edge case where node title is set to 0.

star-szr’s picture

Status: Needs review » Needs work

Thanks @aaronschachter! Test case looks good at least from reading it, one nitpick:

+++ b/core/modules/node/src/Tests/NodeTitleTest.php
@@ -65,5 +65,18 @@ function testNodeTitle() {
+    $xpath = '//title';
+    $this->assertEqual(current($this->xpath($xpath)), 0 . ' | Drupal', 'Page title is equal to 0.', 'Node');

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

aaronschachter’s picture

Assigned: Unassigned » aaronschachter

Sure @cottser I can re-post... Buuut... I was following the same pattern that already exists in the NodeTitleTest, in line 53-54:

xpath = '//title';
$this->assertEqual(current($this->xpath($xpath)), $node->label() .' | Drupal', 'Page title is equal to node title.', 'Node'); 

What's considered best practice here? It would make sense to update that original assertEqual statement 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?

aaronschachter’s picture

Assigned: aaronschachter » Unassigned
Status: Needs work » Needs review
StatusFileSize
new920 bytes

Hmm, 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.

star-szr’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs backport to D7

I did a git bisect and 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.

Is it best practice to create a separate issue for it and leave the original as is?

Sounds good to me.

  • catch committed d805f52 on 8.x
    Issue #578400 by aaronschachter, melsi, Gábor Hojtsy, valthebald,...
catch’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed/pushed to 8.x, thanks!

Moving to 7.x for backport.

joshi.rohit100’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new3.71 KB

Patch for D7.
Please check.

star-szr’s picture

Status: Needs review » Needs work

Thanks @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).

  1. +++ b/includes/theme.inc
    @@ -2504,9 +2504,10 @@ function template_preprocess_html(&$variables) {
    -  if (drupal_get_title()) {
    +  $title = drupal_get_title();
    +  if (isset($title)) {
    

    Won't $title always be set here?

  2. +++ b/modules/node/node.test
    @@ -1873,6 +1873,16 @@ class NodeTitleTestCase extends DrupalWebTestCase {
    +    ¶
    

    Looks like a stray tab character here, please remove per https://drupal.org/coding-standards#indenting.

amitgoyal’s picture

Status: Needs work » Needs review
StatusFileSize
new794 bytes

Please see test-only version of the patch which also removes stray tab character there.

Status: Needs review » Needs work

The last submitted patch, 40: 578400-40-tests.patch, failed testing.

star-szr’s picture

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

joshi.rohit100’s picture

Status: Needs work » Needs review
StatusFileSize
new3.71 KB
new372 bytes

Updated the patch. Please review now.

star-szr’s picture

Status: Needs review » Needs work
+++ b/modules/node/node.test
@@ -1873,6 +1873,16 @@ class NodeTitleTestCase extends DrupalWebTestCase {
+    //Test edge case where node title is set to 0.

Ah, @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 :)

pushpinderchauhan’s picture

Status: Needs work » Needs review
StatusFileSize
new3.71 KB
new451 bytes

Updated patch as @Cottser suggested in #44.

star-szr’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, thanks!

star-szr’s picture

Status: Reviewed & tested by the community » Needs work

Hmm, 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.

gvorbeck’s picture

StatusFileSize
new937 bytes

I'm new to fixing issues but I thought I'd give it a try. I fixed it within the bartik theme.

pushpinderchauhan’s picture

Status: Needs work » Needs review

@gvorbeck, thanks!

Once you add any patch also change Status from Need Work to Need Review.

Status: Needs review » Needs work

The last submitted patch, 48: drupal-578400-44.patch, failed testing.

sivaji_ganesh_jojodae’s picture

Status: Needs work » Needs review
StatusFileSize
new591 bytes

Re-rolled patch #48.

Status: Needs review » Needs work

The last submitted patch, 51: display-page-title-578400-51.patch, failed testing.

mtomaizy’s picture

The last submitted patch, 13: zero-patch-with-tests-comment-fixed.patch, failed testing.

valthebald’s picture

joshi.rohit100’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new591 bytes

Re-rolled #51 but it seems same

Status: Needs review » Needs work

The last submitted patch, 57: display-page-title-578400-57.patch, failed testing.

jmev’s picture

I'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.

Status: Needs work » Needs review
dcam’s picture

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

Status: Needs review » Needs work

The last submitted patch, 57: display-page-title-578400-57.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community
makbul_khan8’s picture

StatusFileSize
new564 bytes

Doing 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".

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 66: core-page-title-even-if-title-is-0-578400-66.patch, failed testing.

dinarcon’s picture

Thanks @makbul_khan8. Here is some feedback.

+++ b/includes/theme.inc
@@ -2676,6 +2676,11 @@ function template_process_page(&$variables) {
+  // Display the page title, even if "0" https://www.drupal.org/node/578400
...
+    $variables['title'] = $variables['title']. ' ';
+  }

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)

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 66: core-page-title-even-if-title-is-0-578400-66.patch, failed testing.

mw4ll4c3’s picture

Assigned: Unassigned » mw4ll4c3

Tackling this for #devdays

mw4ll4c3’s picture

Assigned: mw4ll4c3 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new570 bytes

Re-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.")

star-szr’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thank you @mw4ll4c3!

  1. +++ b/includes/theme.inc
    @@ -2680,6 +2680,11 @@ function template_process_page(&$variables) {
    +  // Display the page title, even if "0" https://www.drupal.org/node/578400
    

    We don't usually include links to d.o issues in code comments unless they are @todos.

  2. +++ b/includes/theme.inc
    @@ -2680,6 +2680,11 @@ function template_process_page(&$variables) {
    +  if ($variables['title'] === '0') {
    +    $variables['title'] = $variables['title'] . ' ';
    +  }
    

    This doesn't seem like a proper fix to me, it's just adding whitespace.

yogen.prasad’s picture

Assigned: Unassigned » yogen.prasad
yogen.prasad’s picture

Assigned: yogen.prasad » Unassigned
travis-bradbury’s picture

I 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 like if (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 but is_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.

travis-bradbury’s picture

Status: Needs work » Needs review

Trigger testbot

Status: Needs review » Needs work

The last submitted patch, 76: display-page-title-even-if-0-test-only-578400-76.patch, failed testing.

travis-bradbury’s picture

A new full patch and interdiff with full patch #45.

travis-bradbury’s picture

Status: Needs work » Needs review

Forgot status for testbot's attention again.

Edit: looks like I've got a mistake still.

$xpath = '//h1[@id="page-title"]';
$this->assertEqual(current($this->xpath($xpath)),
  '0',
  'Node title is displayed as 0.',
  'Node');
debug(current($this->xpath($xpath)));

Debug outputs:

SimpleXMLElement::__set_state(array(
   '@attributes' => 
  array (
    'class' => 'title',
    'id' => 'page-title',
  ),
))

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.

Status: Needs review » Needs work

The last submitted patch, 79: display-page-title-even-if-0-578400-79.patch, failed testing.

travis-bradbury’s picture

Status: Needs work » Needs review
StatusFileSize
new867 bytes
new5.18 KB

Should have it fixed this time. Local testing has the test only patch failing both tests with both passing after the full patch.

The last submitted patch, 82: display-page-title-even-if-0-578400-82-test-only.patch, failed testing.

gatorjoe’s picture

I reviewed #82 on a 7.38 install and confirmed that the page title "0" does appear. This patch was successful for me.

HeimdallJHM’s picture

Last patch works properly, attaching images

HeimdallJHM’s picture

Status: Needs review » Reviewed & tested by the community
David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs work

Putting 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?

shubhangibb’s picture

shubhangibb’s picture

shubhangibb’s picture

Assigned: Unassigned » shubhangibb
cchanana’s picture

Assigned: shubhangibb » cchanana
cchanana’s picture

Assigned: cchanana » Unassigned
sowmiya.S’s picture

Hi,
I have worked in article content type to display the title as "0", its working fine.

joyceg’s picture

Tested the patch.

It works for me.

joyceg’s picture

StatusFileSize
new11.18 KB

After applying the patch.

joyceg’s picture

Assigned: Unassigned » joyceg

I would like to work on this issue.

joyceg’s picture

StatusFileSize
new5.16 KB

This patch works for me.

joyceg’s picture

Status: Needs work » Needs review
tim.plunkett’s picture

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

joyceg’s picture

StatusFileSize
new5.29 KB
new4.45 KB

Adding the inter diff and the new patch.

Status: Needs review » Needs work

The last submitted patch, 100: 578400-100.patch, failed testing.

joyceg’s picture

I am not able to understand this error.

Can someone help me explain the defect of this patch?

travis-bradbury’s picture

The failing test is looking for the title output on the page (as opposed to in the head). I think that your problem is here:

+++ b/themes/bartik/templates/page.tpl.php
@@ -99,7 +99,7 @@
+          <?php if (!empty($title) || $title === 0): ?>

We'd need to compare to the string 0, not integer 0.

karthikkumarbodu’s picture

Status: Needs work » Needs review
StatusFileSize
new5.41 KB

Fixed the string comparison and re-uploaded patch.

Status: Needs review » Needs work

The last submitted patch, 104: core-fix-page-title-display-578400-104-7.x-dev.patch, failed testing.

karthikkumarbodu’s picture

Fixed review comments and uploading patch.

therealssj’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 106: core-fix-page-title-display-578400-106-7.x-dev.patch, failed testing.

therealssj’s picture

Issue summary: View changes
StatusFileSize
new867 bytes
new5.16 KB
new3.89 KB

Why don't we use strlen to check for the title length.
It works for all the possible cases while skipping empty strings.

joyceg’s picture

StatusFileSize
new20.29 KB
new21.13 KB
new19.96 KB

Updated the patch. Uploading the interdiff too.
This worked for me.
Adding the screenshot along with the patch.

joyceg’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 110: 578400-109.patch, failed testing.

joyceg’s picture

StatusFileSize
new58.96 KB
new54.83 KB
joyceg’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 113: 578400-113.patch, failed testing.

therealssj’s picture

StatusFileSize
new867 bytes
new5.21 KB
new4.37 KB

Another try, let's see.
Using strlen(trim($title)) as the check

therealssj’s picture

Status: Needs work » Needs review

The last submitted patch, 116: 578400-116-test-only.patch, failed testing.

karthikkumarbodu’s picture

Status: Needs review » Reviewed & tested by the community

All tests are passing. Good work Mehul

joyceg’s picture

StatusFileSize
new15.91 KB

@therealssj, Nice work. The patch was tested successfully.

valthebald’s picture

Issue tags: -Needs tests

Removing 'Needs tests' (since there is a test now)

fabianx’s picture

Status: Reviewed & tested by the community » Needs work

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

  $title = drupal_get_title();
  if (isset($title) && $title !== '') {
     $head_title = 
  }
  else {
     $title = NULL;
  }

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.

therealssj’s picture

StatusFileSize
new3.9 KB

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

therealssj’s picture

StatusFileSize
new5.16 KB

Attached wrong patch file before.

fabianx’s picture

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

therealssj’s picture

@Fabianx I don't think we need that $title = NULL in the else part.
This is how the following lines look

$title = drupal_get_title();
  if (strlen(trim($title))) {
    $head_title = array(
      'title' => strip_tags($title),
      'name' => check_plain(variable_get('site_name', 'Drupal')),
    );
  }
  else {
    $head_title = array('name' => check_plain(variable_get('site_name', 'Drupal')));
    if (variable_get('site_slogan', '')) {
      $head_title['slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
    }
  }
  $variables['head_title_array'] = $head_title;
  $variables['head_title'] = implode(' | ', $head_title);

I am not sure how $head_title is processed further but from the above lines it does look like that we pass$title in the $head_title array 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.

fabianx’s picture

#126: The problem is:

Lets assume drupal_get_title() returns ''.

$title = drupal_get_title(); // $title === ''

Now do:

if ($title) {
  echo "I have a title."
}

works as it won't display the title.

if (isset($title)) {
  echo "I have a 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:

if (strlen(trim($title))) {
}
else {
  // Reset the title to NULL, so it can be checked with isset().
  $title = NULL;
}

Afterwards:

if ($title) {
  echo "I have a title."
}

works as it won't display the title, as NULL fails this check (BC for existing templates).

if (isset($title)) {
  echo "I have a title."
}

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!

costellofax’s picture

@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?

fabianx’s picture

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

  • catch committed d805f52 on 8.3.x
    Issue #578400 by aaronschachter, melsi, Gábor Hojtsy, valthebald,...

  • catch committed d805f52 on 8.3.x
    Issue #578400 by aaronschachter, melsi, Gábor Hojtsy, valthebald,...
hgoto’s picture

StatusFileSize
new1.48 KB
new6.23 KB

I'd like to chime in and proceed on this issue.

If I understand it correctly, the situation is as following, right?

as is:

  • node title = '0' ... not displayed (the bug)
  • node title = '' (empty string) ... not displayed
  • node title = ' ' (string only with spaces) ... displayed (this is also a bug to be fixed in this issue?)
  • node title = NULL ... not displayed

to be:

  • node title = '0' ... displayed
  • node title = '' (empty string) ... not displayed
  • node title = ' ' ... (string only with spaces) not displayed
  • node title = NULL ... not displayed

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 $title in the page.tpl.php template is prepared not in temlate_preprocess_html() but in template_process_page() and I added the following lines into the patch instead of adding else section into temlate_preprocess_html(). I believe this is a right approach.

@@ -2697,7 +2698,8 @@ function template_process_page(&$variables) {
     $variables['breadcrumb'] = theme('breadcrumb', array('breadcrumb' => drupal_get_breadcrumb()));
   }
   if (!isset($variables['title'])) {
-    $variables['title'] = drupal_get_title();
+    $title = drupal_get_title();
+    $variables['title'] = strlen(trim($title)) ? $title : NULL;
   }

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.

hgoto’s picture

Status: Needs work » Needs review

The last submitted patch, 132: node-title-578400-132-test_only_should_fail.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 132: node-title-578400-132.patch, failed testing.

The last submitted patch, 132: node-title-578400-132-test_only_should_fail.patch, failed testing.

The last submitted patch, 132: node-title-578400-132.patch, failed testing.

fabianx’s picture

Thanks for your work on this!

Nice work on the ?: I almost missed that you implemented my solution :).

I wonder why tests fail though ...

The last submitted patch, 132: node-title-578400-132-test_only_should_fail.patch, failed testing.

hgoto’s picture

Status: Needs work » Needs review

I'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 $title variable with value NULL in page.tpl.php when strlen(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.

fabianx’s picture

Assigned: joyceg » Unassigned
Status: Needs review » Reviewed & tested by the community
Issue tags: -Novice +Needs change record

This 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!

stefan.r’s picture

Issue tags: +Pending Drupal 7 commit

  • stefan.r committed 2051d8c on 7.x
    Issue #578400 by tbradbury, therealssj, joshi.rohit100, aaronschachter,...
stefan.r’s picture

Issue tags: -Pending Drupal 7 commit

Committed and pushed to 7.x, thanks!

This still needs a change record.

fabianx’s picture

Issue tags: +7.51 release notes

Adding for release notes, tag

David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -7.51 release notes

In 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?

fabianx’s picture

I 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

  • catch committed d805f52 on 8.4.x
    Issue #578400 by aaronschachter, melsi, Gábor Hojtsy, valthebald,...

  • catch committed d805f52 on 8.4.x
    Issue #578400 by aaronschachter, melsi, Gábor Hojtsy, valthebald,...

Status: Needs work » 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.