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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mandclu’s picture

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

mandclu’s picture

FileSize
854 bytes

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

mandclu’s picture

Assigned: Unassigned » mandclu
Status: Active » Needs review

Forgot to update the status.

mandclu’s picture

Issue tags: +SprintWeekend2013
mradcliffe’s picture

Status: Needs review » Needs work

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

@@ -149,7 +149,7 @@ function hook_comment_publish(Drupal\comment\Comment $comment) {
 }
 
 /**
- * Respond to a comment being unpublished by a moderator.
+ * Act on a comment that is being saved in an unpublished state.
  *
  * @param Drupal\comment\Comment $comment

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.

@@ -139,7 +139,7 @@ function hook_comment_view_alter(&$build, \Drupal\comment\Plugin\Core\Entity\Com
 }
 
 /**
- * Respond to a comment being published by a moderator.
+ * Act on a comment that is being saved in a published state.
  *
  * @param Drupal\comment\Comment $comment

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.

gyuhyon’s picture

I've created a documentation patch with a little bit more details and attached.

gyuhyon’s picture

Status: Needs work » Needs review

I checked the core and found both hook_comment_publish() and hook_comment_unpublish() is actually used .

I changed the status ^^

gyuhyon’s picture

Sorry there was an error with white spaces.

rteijeiro’s picture

Status: Needs review » Reviewed & tested by the community

It's a RTBC for me!

webchick’s picture

Component: comment.module » documentation
Assigned: mandclu » jhodgdon
jhodgdon’s picture

Assigned: jhodgdon » Unassigned
Status: Reviewed & tested by the community » Needs work

This documentation on the @param is not right. @param documentation should explain what the hook parameter is. Saying

+ *   This hook is invoked every time a comment is saved and
+ *   changes the status of the comment to published,
+ *   whether it was previously in a published status or not.

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.

cilefen’s picture

sushyl’s picture

Status: Needs work » Closed (cannot reproduce)

This issue seems to be outdated. The hooks hook_comment_publish()/hook_comment_unpublish() are not longer present here.

jhodgdon’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Closed (cannot reproduce) » Active

Well we probably need to fix it in Drupal 7 and 6 then.

poker10’s picture

This 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 and hook_comment_unpublish.

joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

RTBC #17,

Thanks @poker10

might as well jam this in.

https://www.drupal.org/node/3324532 could be updated with this change.

  • mcdruid committed e24f6d3e on 7.x
    Issue #1777166 by gyuhyon, mandclu, poker10, jhodgdon, mradcliffe, catch...
mcdruid’s picture

Status: Reviewed & tested by the community » Fixed

One of the tests is being a bit stubborn but the failures look unrelated, especially as we're only changing comments here.

Thanks everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.