In some cases, node views results in an error:
notice: Undefined property: stdClass::$comment_count in modules/comments/comments.module on line 724
This can for example be the case if you use Page manager for displaying nodes, choose not to display the full node content, but display the comment count for the node.
This thread contains a D8 version of the patch described in #1020658: Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 724 of /... (as soon as this issue gets a node ID). It solves the problem by wrapping one of the if statements in an empty() function – which is also done at most other places in comment.module.
Comment | File | Size | Author |
---|---|---|---|
#3 | 1020658-27-extra_empty_check_for_comment_count.patch | 927 bytes | Itangalo |
Comments
Comment #1
Itangalo CreditAttribution: Itangalo commentedPatch attached. Cred to parraccourci and Azol.
Comment #2
Itangalo CreditAttribution: Itangalo commentedSilly me. Patch attached!
Comment #3
Itangalo CreditAttribution: Itangalo commentedRe-roll of the patch in #3 over at #1020658: Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 724 of /.... Applies to D7 dev (as of 110712).
Cred to parraccourci and Azol.
Comment #4
erikhopp CreditAttribution: erikhopp commentedThis applied cleanly (to Drupal 7) and fixed the problem I was having.
Comment #5
xjmThanks for the patch!
At the least, we need an automated test for the bug this fixes. I don't quite fully understand what that is, though... Can anyone provide some steps to reproduce without contrib?
Comment #6
Itangalo CreditAttribution: Itangalo commentedI don't know any way to reproduce this without contrib, as it is now. Sorry. :-/
Comment #7
xjmWell, usually these sorts of bugs are the result of contrib not checking the data it passes to the API, and so they should usually be fixed in contrib that way. That's not always the case, though (and here it sounds like this particular line is inconsistent with the rest of the module), so what's some generic code to reproduce? That code would translate into a test case.
Comment #8
Itangalo CreditAttribution: Itangalo commentedxjm: Good point.
I'll have to try this out before stating anything for certain, but I think you'd get the warning by loading a node, not preparing it to be viewed, and then checking the value of $node->comment_count. Maybe this needs to be on a node that doesn't have any comments.
I hope to get back with some code that can test this soon. Thanks for the feedback.
Comment #9
pounardThe check should be
isset($node->comment_count)
instead ofempty($node->comment_count)
, theisset()
version is safer and won't raise PHP notices if the object property does not exist. All the code everywhere in the Comment module assumes that for every node with comments enabled, this property exist, but seecomment_node_insert()
(and_comment_update_node_statistics()
is quite similar):Both
comment_node_insert()
and_comment_update_node_statistics()
functions will wrap this table records insert or update intovariable_get('comment_maintain_node_statistics', TRUE)
. This variable is TRUE per default, but if we set FALSE here, the code may potentially leak PHP warnings or errors everywhere because the rest of the Comment module don't seem to care about: the only two references of this variable are in those two functions.If we look at the
comment_node_load()
function:It's clear that the code seems to assume every node will have a record in there, which is not true because this table state depends on the
comment_maintain_node_statistics
variable.In my case, the variable is set to TRUE, but it seems that the heavy migrate jobs I'm doing are missing this table, I don't know why, so every node is broken.
While my own experience with this PHP warning is probably due to contrib, it will eventually happen with vanilla core as soon as the site admin just set the variable to FALSE, and may continue to happen when he reverts this variable to TRUE and nodes inserted in between have no new comments.
There is two "clean ways" to fix it:
comment_node_load()
to fill in void but existing object properties on nodes that don't have their own lines into thenode_comment_statistics
table. This is not the proper way because it doesn't care about the settings really, but it would work (and may return false negatives in node listings, saying 0 comments where the node could have some).comment_maintain_node_statistics
, and do smarter tests on node object properties usingisset()
, because at anytime the data state could be altered either by contrib, either by any other bug in core.PS: Wrote that because I experienced the bug on D7, so the priority here is to fix the bug on D7 first IMHO (this is not a feature which needs backports, it is a bug report on the actual stable too, plus if the new entity handling OOP API, I don't know if this bug fits with 8.x anymore).
Comment #10
Dave ReidI think I have found *why* this problem happens in #1892970: Updating node comment statistics should use db_merge otherwise $node->comment_count will be undefined where I'm going to fix it with an upgrade function to fix bad data.
Comment #11
andypostthere's too many ways to get stuck with broken stats, but we need tests first