Node ID from hook_comment_insert 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()-&gt;getParameter('node');
    if ($node instanceof \Drupal\node\NodeInterface) {
        $nid = $node-&gt;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.
print_r($variable) does not return anything useful, it just outputs text (which it does successfully, so it returns boolean TRUE [which is rendered as 1 when printed]). Your drupal_set_message basically does something like echo ($string . $boolean . $string)

Use print_r($variable, true) to return the output of the print_r to the calling function, instead of rendering it.

See https://www.php.net/manual/function.print-r.php