I've read the answers to How to get the node id the comment is attached to in hook_comment_insert? and found how to extract the nid of the node the comment is associated with.

However, I also need to get the cid of the comment itself from inside hook_comment_insert(). How do I do that?

1 Answer 1


All entities have an id() method:

$cid = $entity->id();
  • 1
    Clive's answer is correct. It would be worth adding a link to the documentation to give more context to the answer though. Key things are that the hook fires after the entity has been saved and that you can't save any edits in the hook. i.e., it's useful to examine and react to the insert. api.drupal.org/api/drupal/…
    – Gold
    Mar 14, 2021 at 20:50
  • Thanks! Also thanks to @Gold for the link to the API docs. Mar 15, 2021 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.