Drupal 8 - Retrieve current node

By k, 9 September, 2020
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
  $nid = $node->id();
}