Get the ID that for the node that has been just created I am trying to get the node ID that I have just created. If I do the following code from this answer. 

$node = \Drupal::routeMatch()-&gt;getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
  // You can get nid and anything else you need from the node object.
  $nid = $node-&gt;id();
}


I implemented hook_ENTITY_TYPE_view() (just to test the code), but it gets the ID for all the nodes, while I want the ID of the node that has been created to modify a field value for that node. Is this possible?

Should I use the code I shown in a hook_form_alter() implementation or maybe inside a form submission handler?
If you need to alter the value of a node field right after the node has been created, you don't need that code. You just need to implement hook_ENTITY_TYPE_presave() and verify the node is new.

function mymodule_node_presave(EntityInterface $entity) {
  if ($entity-&gt;isNew()) {
    // The node has been created.
    // Access the node field using $entity.
  }
}


If then, for any reason, you need the node ID, you can access it with $entity-&gt;id().

I used hook_ENTITY_TYPE_presave(), assuming the node needs to be saved with the new field value.