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()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
// You can get nid and anything else you need from the node object.
$nid = $node->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?