How to determine if loading a form or submitting a form in hook_form_alter? I am trying to put a simple message at the top of a node edit form when a user is creating a new node.
I added a \Drupal::messenger()-&gt;addMessage() inside a my form_alter hook but this prints when I open the edit form (good!) but also on the view of the node after the node is saved (bad!).

Looking in debugger I can see that the hook is run twice (load and submit); but I don't see any way of determining which version is being run.

The only thing apparent to me is that $form_state-&gt;requestMethod is different in both cases; but this is a protected property; so no access to it.

code shouldn't be required for this question, but:

function mymodule_form_alter(&amp;$form, $form_state, $form_id) {
  \Drupal::messenger()-&gt;addMessage(t('some msg'), 'warning');
  return;
}
When there is no user input the form is loading for the first time and it is not submitting. 

Additionally you can check the entity whether it is new, if you don't want this condition to be true when the form is loading for an existing node:

if (!$form_state-&gt;getUserInput() &amp;&amp; $form_state-&gt;getFormObject()-&gt;getEntity()-&gt;isNew()) {
  // first load of the form
}