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.
I was getting the error message:
notice: Undefined property: stdClass::$comment_count in modules/comments/comments.module on line 724.
I simply solved it by replacing line 724 by:
if ((!empty($node->comment_count) && user_access('access comments')) || user_access('administer comments')) {
This looks like something that was forgotten since:
(!empty($node->comment_count) && user_access('access comments'))
is the way this is checked everywhere else in the module.
[EDIT]
solution
Read this issue to solve this problem: http://drupal.org/node/137458#comment-5072066
Comment | File | Size | Author |
---|---|---|---|
#27 | 1020658-27-extra_empty_check_for_comment_count.patch | 927 bytes | Itangalo |
#3 | 1020658-comment-count.patch | 1.03 KB | Azol |
Comments
Comment #1
droplet CreditAttribution: droplet commentedso how to reproduce this error, thanks
Comment #2
parraccourci CreditAttribution: parraccourci commentedI have actually no idea and have no time to spend on that.
I'm not sure how the comment module works but it looks like $node->comment_count might not be set in some case.
What i propose here is to replace "!$node->comment_count" by "!empty($node->comment_count)" which has the same effect (see below) and makes this part of the code more robust. plus the way i propose is already used in other parts of the same file, it can't be wrong.
Comment #3
Azol CreditAttribution: Azol commentedI experience the same error.
Here is the patch with solution proposed in #2.
Comment #4
scottrouse CreditAttribution: scottrouse commentedI reported this error at #1036668: Undefined property: stdClass::$comment_count in comment_node_page_additions() when I couldn't find this issue.
I've tested the patch in #3, and it resolves the issue for me.
Thanks.
Comment #5
droplet CreditAttribution: droplet commentedokay. so it's a popular error ? anyone can share how to reproduce this error to other drupaler.
#2, #3 always works.. because it checks the error at error line. But the comment_count should always there, we need to figure out why missing. otherwise we will add empty checks everywhere in drupal.
Comment #6
scottrouse CreditAttribution: scottrouse commentedFor me, it's been hard to specify exactly what began to trigger the error. I can say that, without the patch in #3, I see the error on each individual node page (node/[nid]), but not on any overview pages (front, taxonomy/term/[tid], Views pages, etc). I see it on node pages regardless of whether comments are open, closed or hidden.
If there is more about my particular installation I can provide which may be helpful, please let me know, and I will be happy to do so.
Thanks.
Comment #7
Azol CreditAttribution: Azol commentedThe code in question belongs to
function comment_node_page_additions($node)
, but let us take a look at thefunction comment_node_view($node, $view_mode)
:line 627:
if (!empty($node->comment_count)) {
which looks totally reasonable to me;
line 675:
if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE || (!empty($node->comment_count) && user_access('access comments'))) {
same;
lines 706-708:
which call our "questionable" comment_node_page_additions, but the reason is: this function is called no matter if $node->comment_count is empty or not. So, similar to previous IF statement in line 627, we cannot safely use $node->comment_count condition.
Either the logic of function comment_node_view is broken, or the code in question is merely an overlook.
I wasn't able to reproduce this error with mint D7 install, still trying to figure it out.
Also, I am looking at this code (starting with line 1241 of comment.module):
By default, comment_count column of node_comment_statistics table should contain 0 (unless there are comments posted), which IS actually the case on affected site, so all clear here (I believe). Or not?
Comment #9
geerlingguy CreditAttribution: geerlingguy commentedJust had this problem crop up on one of my sites, after trying to delete a node (but when deleting the node, the redirect module threw an error, so the node didn't actually delete). Now it shows up on every page load.
Here's the error I got when trying to delete the node:
Obviously, the patch above fixes the problem for me, but doesn't solve the overriding issue. I think it may have been the fact that my node wasn't deleted thorougly/properly due to the redirect error.
[Edit: Doing some more checking (this error only started cropping up after I deleted that node)... will post results below:]
Sounds like what might be happening is that if node_delete() (or whatever it is in D7) errors out mid-deletion, this error can crop up, because the node's entry in the node_comment_statistics table is deleted, but the actual node is not deleted.
Comment #10
j0rd CreditAttribution: j0rd commentedI also can not delete my nodes I've created programatically under Drupal 7.
They've been created with no matching entry in `node_comment_statistics` which I assume is the problem. Here's my code for creating my nodes:
I get this warning when viewing my node
Unless I'm doing something wrong here when creating my nodes, I believe this is a bug and should be resolved. I would assume though that using `node_object_prepare()` would initialize things properly, so that I can save my node with out error.
Comment #11
j0rd CreditAttribution: j0rd commentedAdditionally, I noticed that upon my first attempt to delete a node which had an entry in the `node_comment_statistics` table, the entry in `node_comment_statistic` was deleted, but the node was not. Thus causing this error to happen.
Comment #12
j0rd CreditAttribution: j0rd commentedOk, so I did a little more digging.
Turns out I had a bad module (my own!), which was doing a var_dump() and exit() on hook_node_delete(). I forgot it was there.
This was causing the node to ever get deleted, and then caused this error message:
This comes down to the node.module's `node_delete_multiple($nids)` function.
The try catch in there will could call comment_node_delete and then another one later down the chain would toss an exception, thus removing the entry from the `node_comment_statistics` table.
In the exception handling we're calling `$transaction->rollback();`, but I'm not familiar enough with D7 yet to understand if that will re-insert the entry into `node_comment_statistics`. If not, we'll be left in an un-desirable state.
Comment #13
nortont CreditAttribution: nortont commentedI too am getting this error.
For me it occured as soon as I enabled the Comments module. If I disable the module, the error goes away.
I am getting additional errors when I enable the module that may be related and I have not found an answer to yet
Any suggestions?
Comment #14
pillarsdotnet CreditAttribution: pillarsdotnet commentedYeah. Alter your database so that field 'last_updated' has a default value.
Comment #15
nortont CreditAttribution: nortont commentedReplacing line 724 with the following worked as per point 1
if ((!empty($node->comment_count) && user_access('access comments')) || user_access('administer comments')) {
The patch did not work
Comment #16
sirkitree CreditAttribution: sirkitree commentedThe change suggested in #15 worked for me.
Comment #17
pillarsdotnet CreditAttribution: pillarsdotnet commentedThis is getting rediculous.
The originally reported problem occurred because the original poster had a bad module with invalid code. Once he corrected his module, the error vanished. See his report in #12, at which point he should have marked this issue "Fixed."
The error reported in #13 has nothing whatsoever to do with this issue. It is happening because the node_comment_statistics table has an outdated schema, with a "last_updated" field that is no longer supported by the current code. The fix is to either manually correct the database or completely uninstall and reinstall the comments module. (Note that to "uninstall" you must first "disable", then click on the "Uninstall" tab of the modules page.)
The change in #15 is identical to the patch in #3, which turned out to be unnecessary, since it partially obscured the real problem, discovered in #12.
I'm gonna mark this problem "Won't fix; works as designed" because I'm absolutely not convinced there is an error in the current development version of the code.
Comment #18
Azol CreditAttribution: Azol commented...which in no way explains the inconsistent use of $node->comment_count throughout the code, described in #7.
Comment #19
pillarsdotnet CreditAttribution: pillarsdotnet commented@Azol -- If you want to generally clean up the code for consistency, then please submit a D8 patch. After it is accepted, feel free to submit a backport patch to D7.
But please do so in a separate issue. This issue is about a possible bug in the existing code that may have triggered a PHP notice. It turns out that the notice was triggered by a bug in another module, not by a bug in Drupal core. Indeed. your suggested change to Drupal core, though otherwise salutary, may make it harder to discover unrelated bugs in other modules, such as the one found in #12.
Meanwhile Drupal 7.x is now "stable", which means that patches are very unlikely to be accepted unless:
-- or --
Comment #20
bryancasler CreditAttribution: bryancasler commentedWas another issue created in response to #19 that I can follow?
Comment #21
Azol CreditAttribution: Azol commentedNone that I am aware of.
None of my sites now have this problem and there is no known way to reproduce it.
Comment #22
bryancasler CreditAttribution: bryancasler commentedI think this conversation was continued over here. #1171568: Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 721 I put in my two cents about how I ended up getting the error.
Comment #23
sammyframson CreditAttribution: sammyframson commentedSeems to be resolved with the solution in the original post / reply #15...Thanks.
Comment #24
sammyframson CreditAttribution: sammyframson commentedComment #25
remaxos CreditAttribution: remaxos commentedI had same problem
"Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 724 of /home/lamedice/public_html/modules/comment/comment.module)"
I solved so like in post #15...
i changed the line 724 whith this:if ((!empty($node->comment_count) && user_access('access comments')) || user_access('administer comments')) {
and worked !
Comment #26
Itangalo CreditAttribution: Itangalo commentedThere is now an issue about this over at #1215980: $node->comment_count may not be set, depending on comment_maintain_node_statistics variable, comment module is broken, with a patch for D8.
Comment #27
Itangalo CreditAttribution: Itangalo commentedRe-roll of the patch in #3, in Git style.
Cred to parraccourci and Azol.
Comment #28
sctech CreditAttribution: sctech commentedI had this issue with D7 after migrating tables from a staging server. My issue was based on the fact that I was going from one content name to another and the comment fields were set differently. The new content name was more appropriate for our application.
Server #1 - Source
Content_type_1 had comments set read only
Tables node and node_revisions were showing field "comments" = 1
//Whether comments are allowed on this node (at the time of this revision): 0 = no, 1 = closed (read only), 2 = open (read/write).
Server#2 - Target
My new Content_type_2 had comments set off
Tables node and node_revisions showing field "comments" = 0
In phpmyadmin I ran two queries on Server#2.
I went to table node and ran UPDATE node SET comment = 0
Then I went to table node_revisions and ran UPDATE node_revisions SET comment = 0
Hope this helps someone save an hour.
Comment #29
TimelessDomain CreditAttribution: TimelessDomain commented#27 patch fixed error for me
Comment #30
erikhopp CreditAttribution: erikhopp commented#27 patch also fixed this issue for me.
Comment #31
dmon000 CreditAttribution: dmon000 commented#27 fixed problem for me also. Thanks!!
Comment #32
killua99 CreditAttribution: killua99 commented#27 patch fixed error for me
Comment #33
Azol CreditAttribution: Azol commentedI do not doubt it fixes it for you (well, it kinda supposed to :), but it worth mentioning that this issue was prompty closed and it won't be fixed this way unless someone can figure out the way of reproducing the issue.
Check the http://drupal.org/node/137458#comment-5072066 for possible solution fixing this problem without hacking the core. It bears D5 tag, but seems like problem still stands and can sometimes appear while migrating from one major version to the next one or as a result of DB engine misbehaving.
Comment #34
czfed CreditAttribution: czfed commented#27 fix my problem, thx!!
Comment #35
Courtney.B CreditAttribution: Courtney.B commentedI received this error after I added a comment form and comments section to a Node Template. Using Views, Page Manager, and Panels.
Followed #17's advice and problem is fixed.
Comment #36
marcen0 CreditAttribution: marcen0 commentedHi!
I have had the same problem when I was creating comment content. non-administrative users would comment and get the error instead of their comment, but the comment had been stored in the db. As i see patch #27 does not solve the issue because I did some debugging using
which took away the error, but the non-administrative user could not view the comment and nothing was being installed in "node_comment_statistics" table. So i think the problem reside in whichever php class or function is responsible for connecting "node" and "node_comment_statistics" tables.
Comment #37
amirtaiar CreditAttribution: amirtaiar commentedThank you,
That solve my problem.
I got the error after allowing user permission to manage comments.
Comment #38
jeffheaton CreditAttribution: jeffheaton commented#33 worked for me too. No core hacking needed!
Comment #39
TechNikh CreditAttribution: TechNikh commented#33 worked for me too.
http://drupal.org/node/137458#comment-5072066
Comment #40
Dave ReidFYI this is a core bug: #1892970: Updating node comment statistics should use db_merge otherwise $node->comment_count will be undefined.
Comment #41
manoloka CreditAttribution: manoloka commentedI can confirm that 33# fixed my issue
Thanks
Comment #41.0
manoloka CreditAttribution: manoloka commentedAdded solution
Comment #42
func0der CreditAttribution: func0der commentedAll who are using #33, please be aware of this: https://www.drupal.org/node/1036668#comment-8955831
Comment #43
drupixMaybe the correct solution ???
I had the same problem with a custom module who create custom content. Until now, I only used the UI to create custom content and I never had this problem.
After changing the weight of my module to "1" (higer than comment.module) the problem disapear for new custom content created.
To resolve the problem for custom nodes created before I changed the weight of my modules, I had to use the script #8.
Now, everything works fine.
Comment #44
func0der CreditAttribution: func0der commentedSince there is no proper way to set the weight of a module from installation on, I would say that A solution, but not THE solution for the cause of the problem.
Comment #45
blogers CreditAttribution: blogers commentedthis work for me #8 https://www.drupal.org/node/1036668#comment-6085070
Comment #46
MahmoodZidan CreditAttribution: MahmoodZidan commented#17 thanks for your comment.
It was a bad idea to play with the core. Your solution worked like charm. I was afraid I'll lose all the comment settings when I uninstall but it actually didn't.
Comment #47
keramsey CreditAttribution: keramsey commentedI am getting the same error with Drupal 7.64 on only one content page (on 1 domain access page, but not the rest of the domains the page is on). I do not have a 'last_updated' field in my node_comment_statistics table as described in #17. I also ran SQL script in #45 even though the table was not truncated. The nid of the content that had the error was not found in the node_comment_statistics table.
I manually applied the changes in #1 which removed the error. I am concerned about patching core in this way. Does anyone have any idea how I can search for a possible offending module as described in #17?
Could this be a Domain Access issue?