I need the ID of the node the inserted comment is attached to. This is what I have tried:
function my-module_comment_insert(Drupal\Core\Entity\EntityInterface $entity)
{
// According to https://drupal.stackexchange.com/questions/145823/how-do-i-get-the-current-node-id
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
$nid = $node->id();
}
drupal_set_message("The vaue is " . print_r($nid), 'warning');
}
But... The value I get is "1", while it should be (according to node URL) "55". Or am I using drupal_set_message for debugging the wrong way?
I also tried $entity->get('entity_id')->target_id (according to: How to get the node id the comment is attached to in hook_comment_insert?) or \Drupal::routeMatch()->getRawParameter('node'), but I always get "1" and I'm positive that the website cache has been cleared.