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.
The comment is being published by the moderator.
What actually happens is the hook is invoked every time a comment is saved and $comment->status == COMMENT_PUBLISHED - whether it was previously in a published state or not.
I got caught out by this when I wanted to trigger something on the change of status, and it ran every time instead.
We should fix the docs. But also we should just drop this hook in Drupal 8 since the only only thing it saves is an if check on $comment->status.
Comment | File | Size | Author |
---|---|---|---|
#17 | 1777166-17.patch | 746 bytes | poker10 |
Comments
Comment #1
mandclu CreditAttribution: mandclu commentedIs the issue really that the documentation needs to change, that the hook should be dropped, or that it should be changed to be something useful?
If I understand your comment correctly, the real issue is that we need a hook that only targets a CHANGE in status, that doesn't fire every time a node is saved with a specified status. For the sake of clarity, I might suggest that we implement a new hook, for example hook_comment_status_change.
Please clarify what action you want taken, and I would be happy to put some sprint equity against this issue.
Comment #3
mandclu CreditAttribution: mandclu commentedHere's a patch to the documentation, which admittedly does not accurately describe the actual behaviour for hook_comment_publish() or hook_comment_unpublish(). Perhaps if there are questions about whether or not the hooks are actually useful or should change those could be posted as new issues.
Comment #4
mandclu CreditAttribution: mandclu commentedForgot to update the status.
Comment #5
mandclu CreditAttribution: mandclu commentedComment #6
mradcliffeNice job on the patch, mandclu. The code and formatting looks good. However I think the entire issue may need to be looked at. See below.
I just looked through the current code, and this still exists, but I don't think hook_comment_unpublish() is in core anymore? It may have been removed more recently than when the patch was uploaded. It is definitely not called in the postSave() method.
I think this is accurate although I question the name of the hook. Why should it be called hook_comment_publish()?
This seems like a duplicate of #900396: Remove separate comment publishing hooks or fix them, which questions the need for these hooks as well.
If we already have a hook for general comment save via Entity, then maybe it would be better to add in logic so it only fires on state transition.
Comment #7
gyuhyon CreditAttribution: gyuhyon commentedI've created a documentation patch with a little bit more details and attached.
Comment #8
gyuhyon CreditAttribution: gyuhyon commentedI checked the core and found both hook_comment_publish() and hook_comment_unpublish() is actually used .
I changed the status ^^
Comment #9
gyuhyon CreditAttribution: gyuhyon commentedSorry there was an error with white spaces.
Comment #10
rteijeiro CreditAttribution: rteijeiro commentedIt's a RTBC for me!
Comment #11
webchickComment #12
jhodgdonThis documentation on the @param is not right. @param documentation should explain what the hook parameter is. Saying
is not explaining what the parameter is, it's explaining what the hook does... or at least attempting to. The way it is worded, it sounds like the *hook* is changing the status of the comment to published, and the wording also says "changes the status to published" then "whether it was previously published or not", which negates the word "changed"..
The whole thing needs some work! Make it clear (don't say the status is changed to published if it isn't changed) and don't put it in the @param docs.
Comment #13
cilefen CreditAttribution: cilefen commented#2554965: hook_comment_unpublish is never invoked
Comment #14
sushylThis issue seems to be outdated. The hooks hook_comment_publish()/hook_comment_unpublish() are not longer present here.
Comment #15
jhodgdonWell we probably need to fix it in Drupal 7 and 6 then.
Comment #16
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedThis is still an issue in D7. #2554965: hook_comment_unpublish is never invoked is committed, so would need to fix doc for both
hook_comment_publish
andhook_comment_unpublish
.Comment #17
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedUploading a D7 patch based on #3.
Comment #18
joseph.olstadRTBC #17,
Thanks @poker10
might as well jam this in.
https://www.drupal.org/node/3324532 could be updated with this change.
Comment #20
mcdruidOne of the tests is being a bit stubborn but the failures look unrelated, especially as we're only changing comments here.
Thanks everyone!